C# 4.0 Features Session 1

Hi Friends,

Today, we’ll discuss the basics of C#. I’ll be taking this on C# 4.0 features most of the time and some normal scenarios, which we generally encounter on daily basis like below. this is a simple example.

//Error, object doesn’t contain anything say Length
object a = “hello world”;
Console.WriteLine(a.Length);

Now, let’s try to fix this

// Valid One, how casting saves the code
object a = “hello world”;
string b = (string)a;
Console.WriteLine(b.Length);

so, now when i run this this will give me output 11 obviously length of the string. so, here casting saves us. Important point casting is evaluated @ run time.

so, now let me take one more example to prove my point that how casting is evaluated @ run time. let’s consider below scenario

// will throw error @ run time saying unable to cast object
//Unable to cast object of type ‘System.Collections.Generic.List`1[System.Int32]’ to type ‘System.String’.
object a = new List<int>();
string b = (string)a;
Console.WriteLine(b.Length);

when you compile the above code, this will compile well, but when you run the same, it will throw the above error.

Now, let’s consider another scenario. so, an object either be null or it’s a way to reach out there. so, if assign that object null and tried to walkthough that object and take out the length property even after casting, that would return exception not empty value.

// Null reference exception, tried to walkthough the null object.
object a = null;
string b = (string)a;
Console.WriteLine(b.Length);

Now, dynamic keyword resolves the thing @ run time. so, if you see below snippet, i tried to print the Length of the string, but while writing i didn’t get the intellisense also it evaluates the things correctly.  so, the thing is same object is utilized as string and also as object of int array and produced the correct result.

// Dynamic keyword sorts the thing out @ run time
dynamic a = “hello world”;
Console.WriteLine(a.Length);
a = new int[] { 1, 2, 3 };
Console.WriteLine(a.Length);

// Strongly typed, these are static types, hence opcode has been evaluted by compiler. -100 will o/p

int a = 100;
int b = 200;
Console.WriteLine(a-b);

// lazy typed
dynamic a = 100;
dynamic b = 200;
Console.WriteLine(a-b);

even this will do the same stuff, but there is whole lot of things going in, you can confirm the same from ILDASM. Important Point Dynamic comes with cost(performance cost).

Now, let’s talk about little bit about named arguments and optional parameters. Let’s consider the below scenario.

//Simple One
static void Main(string[] args)
{
x(“Hello C#”); // provided argument
}

static void x(string y) // declared parameter, kind of local variable
{
Console.WriteLine(y);
}

Now, in case of named arguments, i could go ahead and pass the value with variable name as shown below. also, it doesn’t matter how you are doing ordering and all. if you have assigned the same with name, that’s it your job is done. compiler will position it accordingly.

// Named Arguments
static void Main(string[] args)
{
x(z:”world”, y:”Hello”); // provided argument //named argument doesn’t matter ordering
}

static void x(string y, string z) // declared parameter, kind of local variable
{
Console.WriteLine(y+z);
}

//Optional Paramter, it helps us to skip writing additional overload methods like below
// here the output would be worldworld,rahulworld,daveworld,scottworld,helloworld
//but, the thing is you can’t have 1st paramter as optional if it’s alone
static void Main(string[] args)
{
x(“world”); // provided argument //named argument doesn’t matter ordering
x(“Rahul”);
x(“Dave”);
x(“Scott”);
x();
}

static void x(string y=”Hello”, string z=”World”) // declared parameter, kind of local variable
{
Console.WriteLine(y+z);
}

x(y:”world”); // provided argument //named argument doesn’t matter ordering
x(y:”Rahul”);
x(y:”Dave”);
x(y: “Scott”);
x();
// this will also produce you the same result.
// But, here is a catch you can’t put named parameter 1st and then the positional one, it will throw error
x(z: “Scott”, “tom”); // Invalid one

I hope, this session could have been useful. we’ll d deep dive in next C# sessions. Till then stay tuned and happy coding.

Thanks,
Rahul
Happy Coding

1,314 total views, no views today