Created
May 23, 2023 07:02
-
-
Save kinarajv/bf251417fa8a313e1c98a43fe7296f41 to your computer and use it in GitHub Desktop.
Dynamic vs Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Object | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
int x = 5; | |
dynamic obj1 = x; // boxing | |
Console.WriteLine(obj1); | |
int a = 10; | |
dynamic obj2 = a; | |
//string str = obj2; //will throw an exception if not use ToString() | |
string str = obj2.ToString(); | |
Console.WriteLine(str); | |
int c = 11; | |
dynamic obj3 = c; | |
long d = obj3; | |
Console.WriteLine(d); | |
dynamic obj4 = "Hello, World!"; | |
if (obj4 is string x2) | |
{ | |
Console.WriteLine(x2); | |
} | |
dynamic obj5 = "Hello, World!"; | |
string str5 = obj5; | |
if (str5 != null) | |
{ | |
Console.WriteLine(str5); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Object | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
int x = 5; | |
object obj1 = x; // boxing | |
int y = (int)obj1; // unboxing | |
Console.WriteLine(y); | |
int a = 10; | |
object obj2 = a; | |
//string str = (string)obj2; //will throw an exception if not use ToString() | |
string str = ((int)obj2).ToString(); | |
Console.WriteLine(str); | |
int c = 11; | |
object obj3 = c; | |
//long d = (long)(int)obj3; //no need to cast to long, because will cast explicitly | |
long d = (int)obj3; | |
Console.WriteLine(d); | |
float e = 12.2f; | |
object obj6 = e; | |
//int i = (int)obj6; //will throw an exception | |
int i = (int)(float)obj6; | |
Console.WriteLine(i); | |
object obj4 = "Hello, World! 1"; | |
if (obj4 is string) | |
{ | |
string str4 = (string)obj4; | |
Console.WriteLine(str4); | |
} | |
object obj5 = "Hello, World! 2"; | |
if (obj5 is string str5) | |
{ | |
Console.WriteLine(str5); | |
} | |
object obj7 = 23; | |
int j = obj7 as int? ?? 0; | |
Console.WriteLine(j); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment