C# .NET - What is Casting in C#

Asked By Arth Srivastava
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#  Casting in C#

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:  Casting:

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...  cehck here...

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...  Issue...
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#  Casting in C#
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  hmm
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  Check this
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  best link
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  Custom casting
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  Custom casting
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/
Create New Account
help
RootDataHandler has 1 DataHandlers [11 / 17 / 2011 19:11:17.0914] [0] Setup encountered an exception when trying to access the setup state file: Could not find file 'C: \ ExchangeSetupLogs \ exchangeInstallState.xml'. [11 / 17 / 2011 19:11:40.0020] [0] Setup is determining what bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:14:14.0386] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreConfiguration-20111117-13141306991562273802.ps1' [11 / 17 / 2011 19:14:14.0386] [1] Executing 0417] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:14:14.0417] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:14:14.0417] [1] Executing: $RoleTargetVersion = '14.01.0339.001 bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:15:28.0550] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreSetup-20111117-13152805501269886028.ps1' [11 / 17 / 2011 19:15:28.0550] [1] Executing 0550] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:15:28.0565] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:15:28.0565] [1] Executing: $RoleTargetVersion = '14.01.0339.001 bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:15:28.0706] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreFileCopy-20111117-13152807061586562721.ps1' [11 / 17 / 2011 19:15:28.0706] [1] Executing 0706] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:15:28.0706] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:15:28.0706] [1] Executing: $RoleTargetVersion = '14.01.0339.001
RootDataHandler has 1 DataHandlers [11 / 17 / 2011 19:11:17.0914] [0] Setup encountered an exception when trying to access the setup state file: Could not find file 'C: \ ExchangeSetupLogs \ exchangeInstallState.xml'. [11 / 17 / 2011 19:11:40.0020] [0] Setup is determining what bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:14:14.0386] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreConfiguration-20111117-13141306991562273802.ps1' [11 / 17 / 2011 19:14:14.0386] [1] Executing 0417] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:14:14.0417] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:14:14.0417] [1] Executing: $RoleTargetVersion = '14.01.0339.001 bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:15:28.0550] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreSetup-20111117-13152805501269886028.ps1' [11 / 17 / 2011 19:15:28.0550] [1] Executing 0550] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:15:28.0565] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:15:28.0565] [1] Executing: $RoleTargetVersion = '14.01.0339.001 bin \ EnterpriseServiceEndpointsConfig.xml". [11 / 17 / 2011 19:15:28.0706] [1] Writing informational script to 'C: \ ExchangeSetupLogs \ Start-PreFileCopy-20111117-13152807061586562721.ps1' [11 / 17 / 2011 19:15:28.0706] [1] Executing 0706] [1] Executing: $RoleRoles = 'ClientAccessRole' [11 / 17 / 2011 19:15:28.0706] [1] Executing: $RoleSetupLoggingPath = 'C: \ ExchangeSetupLogs' [11 / 17 / 2011 19:15:28.0706] [1] Executing: $RoleTargetVersion = '14.01.0339.001
Text = = "All" && CheckBoxList1.SelectedIndex = = -1) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area, d.carpet_area, d.bedrooms, d.prop_desc, e.ownership_name from tbl_image_entry a, tbl_state_new b, tbl_city_new c, tbl_new_sale_property d, tbl_ownership_master e where a.user_id = d.user_id and b.state_id = d.state and c.city_id = d.city and a.prop_name = d.prop_name and e.ownership_id = d.prop_ownership and prop_type CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area, d.carpet_area, d.bedrooms, d.prop_desc, e.ownership_name from tbl_image_entry a, tbl_state_new b, tbl_city_new c, tbl_new_sale_property d, tbl_ownership_master e where a.user_id = d.user_id and b.state_id = d.state and c.city_id = d.city and a.prop_name = d.prop_name and e.ownership_id = d.prop_ownership and prop_ownership CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area, d.carpet_area, d.bedrooms, d.prop_desc, e.ownership_name from tbl_image_entry a, tbl_state_new b, tbl_city_new c, tbl_new_sale_property d, tbl_ownership_master e where a.user_id = d.user_id and b.state_id = d.state and
C# / Ping Hi. First I would like to thank all of yoy for your fast repleys System.Runtime.InteropServices; using System.Threading; using System.Data; using System.Data.SqlClient; namespace printHosts { class Program { static void Main( string [] args) { if (args.Length = = 0) { } SqlConnection oconn; SqlCommand ocomm; string Fill(ds, "Hostinfo" ); foreach ( DataRow row in ds.Tables[0].Rows) { address = row[ "hostname" ].ToString(); Console .WriteLine(address); try { sSql = "SELECT COUNT(*) FROM Hostinfo WHERE hostname = '" + address + "'" ; ocomm = new SqlCommand (sSql ip = null ; try { ip = Dns .GetHostEntry(address).AddressList[0]; } catch (System.Net.Sockets. SocketException ex) { Console .WriteLine( "DNS Error: {0}" , ex.Message); Console .WriteLine( "Press any key to exit." ); Console .ReadLine(); return ; } / / Console.WriteLine("Pinging {0} [{1}] with 32 bytes of data:", address, ip.ToString()); Console .WriteLine(); Thread pingThread = new Thread ( new ParameterizedThreadStart (StartPing)); pingThread.Start(ip); pingThread.Join(); } } [ Flags ] enum