Nice reference for C# evolution Part 3

Csharp
Csharp

Nice reference for C# evolution Part 3

Welcome to CodeSpread!

We completed till C# 3.0 introductions in last article and C# 2.0 in this article . We will forward our journey with C# 4.0 features.

C#4.0

Late Binding (Dynamic)

Late binding is always presumed as something which happens during run-time. C# 4.0 came with an approach to perform type binding at runtime. This is a feature which was not available in any of the previous versions. As the previous versions were improvement over each other and didn’t bring new concepts to the table. dynamic is a new concept and fairly an achievement over the existing compile time binding approach. We will go through the existing approaches first,before moving to ‘dynamic’ keyword.

What is static typing or compile-time type binding?

Look at the below code.


string s= “CodeSpread”;

Here we have declared a variable ‘s’ of type ‘string’ to store value ‘CodeSpread’. When we compile this statement, we never get an error as compiler deduce the type of variable ‘s’ during compile time.

Now, Look at another code.


var s= “CodeSpread”;

Here also, On compiling the code, complier deduce the type of variable ‘s’ by looking at the right hand side expression and fix it as ‘string’ during compile time.

Both the approaches describes static typing or compile-time type binding.

What is Late Binding?

Look at this code.


dynamic s= “CodeSpread”;

Here, compiler won’t throw any error but will not be able to determine the actual type of variable ‘s’. At run-time, variable ‘s’ will be resolved to type ‘string’.

As a developer, we get the same freedom which we have while using languages like PHP or javascript, we don’t have to worry whether our code has compiled or not.

Named Arguments

This is also one of the biggest help for a developer as we don’t need to remember the position of the argument while passing it to a method.

Let’s look at an example. Method before C# 4.0


public int CalculateSum (int num1, int num2)

{

return num1+num2;

}

//This method can be called as

int result= CalculateSum ( 5 , 6 );

Method after C# 4.0


//Above method can be called  in C# 4.0 as

int result= CalculateSum ( num1: 5 , num2: 6 );

//Or

int result= CalculateSum ( num2: 5 , num1: 6 );

Optional Parameters

Another relief for developers as parameters passed in a method can be optional plus a default value is specified as part of a declaration.Let’s see the code.


public int CalculateSum (int num1, int num2=5)

{

return num1+num2;

}

//Optional parameter is num2 with a default value 5

//Calling this method

int result=CalculateSum ( 5 , 6 );

//Or

int result=CalculateSum ( 5 );

If we don’t provide the value for num2 parameter, it takes the default value 5.

More COM Support

Till C# 4.0, the code written for an Interop assembly was ugly and as a developer, we really avoided it. But with C# 4.0, object variable, the ref keyword , dependency on huge Primary Interop Assemblies are removed and code looks more cleaner with C# features like dynamic support and optional parameters.


Word.Application d=
new Word.Application() { Visible = true };

object filename = "CodeSpread.docx";

object missing = Type.Missing;

d.Documents.Open(ref fileName, ref
missingValue,//…

Write it with the help of C# 4.0 COM support


var doc = Application.GetDocument("CodeSpread.docx");

//Or

Document d = new Document();

d.SaveAs(FileName: "CodeSpread.docx");

Interaction with COM components was considered a heavy process but with the advent of COM Support in C# 4.0 , This process is very much normalized and became straight-forward.

In next article,we will see C# 5.0 features.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Nice reference for C# evolution Part 2Nice reference for C# evolution Part 2We completed till C# 2.0 introductions in last article. We will forward our journey with C# 3.0 features. C# 3.0 Lambda Expressions: Last article, we discussed anonymous function and how they ex...
  • Nice reference for C# evolution Part 4Nice reference for C# evolution Part 4We completed till C# 4.0 introductions in last article,C# 3.0 in this article and C# 2.0 in first article . We will forward our journey with C# 5.0 features. C# 5.0 Async Feature There are new m...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • C# – Box(ing) and Un-box(ing)C# – Box(ing) and Un-box(ing)I have heard this concept from many people and their different versions so I always wanted to share some insight on it which can work as a one stop solution. What is boxing and un-boxing? The Con...
  • RegisterClientScriptBlock, RegisterClientScriptIncludeRegisterClientScriptBlock, RegisterClientScriptInclude"Current trends are more inclined towards the client model for a better UI experience where small pieces of client script are integrated into the application." These client scripts can be declarat...
  • Nice reference for C# evolutionNice reference for C# evolutionWhile going through some posts on MSDN, I came across this beautiful image which can be termed as a one stop solution for C# evolution. This is a gift reference, I would like to share with my frien...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • RegisterStartupScript, RegisterOnSubmitStatementRegisterStartupScript, RegisterOnSubmitStatementWe have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss thes...