Posts Tagged ‘C# 4.0’

In my last post I had discussed about optional arguments and default values.In continuation to that today we will take a look into the named arguments feature.Let us consider the method definition and invocation as shown below:


class Program {
       static void Main(string[] args) {
           Foo f = new Foo();
           f.PrintName("Sankarsan", "S", "Bose");
           Console.ReadLine();
       }
   }
   public class Foo {
       public void PrintName(string firstName, string lastName, string middleName ) {
           Console.WriteLine("Name is {0} {1} {2}", firstName, middleName, lastName);
       }

}

Here the PrintName method is invoked using what is called positional arguments where the value of the parameters are determined by their position in the parameter list.In C# 4.0 we can do it like this using named arguments:

(more…)

I have recently downloaded the Visual Studio 2010 Beta 1 and started looking into the .NET Framework 4.0/IDE features.In the next couple of posts I will be discussing about these.To start with I have chosen optional parameters and default values.This feature is nothing new and has been there in languages like C++ for quite long.We will first take a look how we can use this in C# and how it is implemented.The following lines of C# code shows a method where the parameter middleName is optional and client is forced to pass a blank string value there as the parameter is not optional.
(more…)