C# .NET - What is Casting in C# - Asked By Arth Srivastava on 08-Sep-08 06:52 AM

Hi All,

  I am a newbie to C#.

 i want to know what exactly is Casting in C#,Please expalin in layman's terms or provide me a good link so that i can study that.

Thanks,

Arth

Casting in C# - SP replied to Arth Srivastava on 08-Sep-08 06:55 AM

Converting between data types can be done explicitly using a cast, but in some cases, implicit conversions are allowed.
For example:

static void TestCasting()
{
    int i = 10;
    float f = 0;
    f = i;  // An implicit conversion, no data will be lost.
    f = 0.5F;
    i = (int)f;  // An explicit conversion. Information will be lost.
}
 

A cast explicitly invokes the conversion operator from one type to another. The cast will fail if no such conversion operator is defined.
You can write custom conversion operators to convert between user-defined types. For more information about defining a conversion operator,
see explicit (C# Reference) and implicit (C# Reference).

Example ::

The following program casts a double to an int. The program will not compile without the cast.

class Test
{
    static void Main()
    {
        double x = 1234.7;
        int a;
        a = (int)x;  // cast double to int
        System.Console.WriteLine(a);
    }
}

See this for further understanding :: http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx

Hope it helps.

Casting: - Binny ch replied to Arth Srivastava on 08-Sep-08 06:55 AM

Converting between data types can be done explicitly using a cast, but in some cases, implicit conversions are allowed.
A cast explicitly invokes the conversion operator from one type to another. The cast will fail if no such conversion operator is defined.
You can write custom conversion operators to convert between user-defined types

Example:
static void TestCasting()
{
int i = 10;
float f = 0;
f = i; // An implicit conversion, no data will be lost.
f = 0.5F;
i = (int)f; // An explicit conversion. Information will be lost.
}

Go through this l;ink:
http://www.developernotes.com/post/Casting-in-C.aspx
http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx

cehck here... - Vasanthakumar D replied to Arth Srivastava on 08-Sep-08 07:02 AM

Hi,

Casting is refered as conversion among different data types.

There are two types of casting

1. Implicit conversion

2. Explicitn conversion

check here for more details

http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx

Issue... - Atul Shinde replied to Arth Srivastava on 08-Sep-08 07:04 AM

Casting means conversion from one datatype to other datatype.

object o = 20;

int x = (int)o;

Here, o is the object having integer value. We need to convert it into integer. So, we need to give (int) i.e. typecast it.

Casting in C# - Kalit Sikka replied to Arth Srivastava on 08-Sep-08 07:13 AM

Converting between data types can be done explicitly using a cast, but in some cases, implicit conversions are allowed. For example:

static void TestCasting()
{
    int i = 10;
    float f = 0;
    f = i;  // An implicit conversion, no data will be lost.
    f = 0.5F;
    i = (int)f;  // An explicit conversion. Information will be lost.
}
A cast explicitly invokes the conversion operator from one type to another. The cast will fail if no such conversion operator is defined. You can write custom conversion operators to convert between user-defined types. For more information about defining a conversion operator

class Test
{
    static void Main()
    {
        double x = 1234.7;
        int a;
        a = (int)x;  // cast double to int
        System.Console.WriteLine(a);
    }
}

Output: 1234

hmm - Santhosh N replied to Arth Srivastava on 08-Sep-08 07:14 AM

Casting is nothing but ability to convert one data type to other type which can be done

For example if a string variable contains 123 as its value, it can be converted to integer type and this can be casted...

on the other hand if the same string variable contians 123str then it can not be converted to integer type...

And there are different ways of casting..

1) Implicit casting

2) Explicit casting

depending on the options of the compiler some languages converts automatically and those are implicit ..

Check this - san san replied to Arth Srivastava on 08-Sep-08 07:16 AM
Hi

Casting means converting between data types. Conversion can be done implecitly. If u want to do it explicitly u need to go for casting (type casting).

There are 2 types of casting. 
1. C-Style casting
2. Casting using "as" keyword

The most obvious difference is that a C style case will throw and exception if the cast fails
and the "as" style cast will simply return null. So first you need to take into consideration
what these differences will have on your code. Will you need to wrap every cast in try/catch blocks?
Not if you use the "as" style, but you will need to check for null.
Example for C-Style / Prefex cast

GenericType g = ... ;
SpecificType t = (SpecificType) g;


      int i = 10;
      float f  = (float) i;


Example for "as" cast

GenericType g =...;
SpecificType t = g as SpecificType;

      int i = 10;
      object  obj = i as object ;


Hope this helps
cheers...
best link - Perry replied to Arth Srivastava on 08-Sep-08 08:01 AM

The best thing is to read http://msdn.microsoft.com/en-us/library/ms173105.aspx where all the types of casting has been given.

Regards,

Paresh

Custom casting - Alejandro Mehring replied to Perry on 02-Jun-09 12:22 PM
Hi!
C# also let's you create your own type conversions using the keywords explicit and implicit
Check this blog entry I wrote on the topic
http://moebiusit.com/blog/alemehring/2009/02/27/custom-type-conversion/
Custom casting - Alejandro Mehring replied to Perry on 02-Jun-09 12:24 PM
Hi!
C# also let's you create your own type conversions using the keywords explicit and implicit
Check this blog entry I wrote on the topic
http://moebiusit.com/blog/alemehring/2009/02/27/custom-type-conversion/