1. Home
  2. Docs
  3. C# Programming
  4. Basics
  5. Casting

Casting

Implicit Casting

private static void ImplicitCasting()
{
    char c = 'A';
    int i = c;
    long j = i;
    float k = j;
    double l = k;
}

Explicit Casting

private static void ExplicitCasting()
{
    short h = (short)c;
    k = (float)l;
    j = (long)k;
    i = (int)j;
    c = (char)i;
}

Type Conversion Class

private static void TypeConversionClass()
{
    string s = "1";
    i = Convert.ToInt32(s);

    string str = "abc";
    var a = 1;
    // a = Convert.ToInt32("abc"); //not valid (input format exception)
}
Was this article helpful to you? Yes No

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *