Home SEO Tools HTML Escape Tool Character Counter Guest post

Latest New features of CSharp

0 comments


Latest New features of CSharp

the following new features to the language:
  • Dynamic programming
  • Named parameters
  • Optional parameters
  • Covariance and Contravariance

Dynamic programming

This new version of the C# language brought a new type dynamic. Once a variable is declared as having type dynamic, operations on these value are not done or verified at compile time, but instead happen entirely at runtime. This is known also as duck typing. The name of the concept refers to the duck test, which may be phrased as follows: "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck". In other words, we are concerned about what the object is doing rather than with the type of the object. The dynamic type is used to simplify access to COM objects, IronPython and IronRuby libraries and to the HTML DOM.
using System;


public class Duck
{
public void quack()
{
Console.WriteLine("Quaaaack");
}
}

public class Person
{
public void quack()
{
Console.WriteLine("Person imitates a duck");
}
}


public class CSharpApp
{
static void Main()
{
Duck donald = new Duck();
Person josh = new Person();

InTheForest(donald);
InTheForest(josh);
}

public static void InTheForest(dynamic duck)
{
duck.quack();
}
}
In our example, we have two classes. The Duck class and the Person class. Both have the quack() method.
public static void InTheForest(dynamic duck)
{
duck.quack();
}
The InTheForest() method has a dynamic parameter. The type of the object is not important; we are concerned about capabilities of the objects. If objects passed to the InTheForest() method can invoke the quack() method, than we are fine.
$ dmcs ducktyping.cs
$ /usr/local/bin/mono ducktyping.exe
Quaaaack
Person imitates a duck
We compile and run the program. We use the Mono dmcs compiler, that is shipped with the Mono 2.8 and supports the C# 4.0 profile.

Named parameters

In earlier versions of the C# language, the arguments were provided in the order in which they appeared in the method's signature. And the position in the parameter list is important when evaluating a method. Named arguments enable us to specify the method parameters by their names. When specifying the named arguments, the position of the parameter is not important anymore.
using System;


public class CSharpApp
{
static void Main()
{
ShowMessage(name: "Jane", age: 17);
}

public static void ShowMessage(int age, string name)
{
Console.WriteLine("{0} is {1} years old", name, age);
}
}
A simple example with named arguments.
ShowMessage(name: "Jane", age: 17);
The parameter name is followed by the colon (:) character and the value. The parameters may not be specified in order of the method signature.
$ /usr/local/bin/mono named.exe 
Jane is 17 years old
Output.

Optional parameters

With C# 4.0 there are required parameters and optional parameters. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Optional parameters are defined at the end of the parameter list, after any required parameters. An optional parameter is created, when when a default value is specified for the parameters.
using System;


public class CSharpApp
{
static void Main()
{
Power(4, 4);
Power(5);
}

public static void Power(int x, int y=2)
{
int z = 1;

for (int i=0; i<y; i++)
{
z *= x;
}

Console.WriteLine(z);
}
}
An example for an optional argument. We have a Power() method. The method takes two parameters; the base and the exponent. If we do not specify the exponent, than the default 2 is used.
public static void Power(int x, int y=2)
In the method definition, we have a required x parameter and the optional y parameter. The optional y has a default value.
Power(4, 4);
Power(5);
When we call the Power() method, we can specify one or two parameters.
$ /usr/local/bin/mono optional.exe 
256
25
Output of the example.

Covariance & contravariance

C# version 4.0 brings covariance for generics and delegate types. They were introduced because they bring flexibility to programming. Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return types). (wikipedia)

Types that are
  • covariant: convert from wider (double) to narrower (float).
  • contravariant: convert from narrower (float) to wider (double).
  • invariant: are not able to convert (Null).
using System;


public class CSharpApp
{
static void Main()
{
string[] langs = {"C#", "Python", "PHP", "Java"};
object[] objs = langs;

object o1 = objs[1];
Console.WriteLine(o1);
}
}
Arrays are covariant from the beginning.
string[] langs = {"C#", "Python", "PHP", "Java"};
We have an array of string.
object[] objs = langs;
We assign a wider array of strings to a narrower object type.
object o1 = objs[1];
Console.WriteLine(o1);
We get an item and print it to the console.
$ ./covariance.exe 
Python
Output of the array covariance example.

In the following example, we have a covariance for generics.
using System;
using System.Collections.Generic;


public class CSharpApp
{
static void Main()
{
IEnumerable<string> strings = new List<string>() {"1", "3", "2", "5"};
PrintAll(strings);
}

static void PrintAll(IEnumerable<object> objects)
{
foreach (object o in objects)
{
System.Console.WriteLine(o);
}
}
}
We have a generic list of strings. We call then the PrintAll() method, which prints all the elements of the list. Note that the method parameter has a less derived type parameter that our generic list.
$ /usr/local/bin/mono covariance2.exe 
1
3
2
5
Output.

The following is an example for a covariance in delegates.
using System;
using System.Collections.Generic;


public class CSharpApp
{
static void Main()
{
Action<string> del = ShowMessage;
del("Proximity alert");
}

static void ShowMessage(object message)
{
Console.WriteLine(message);
}
}
We assign a method which has a narrower parameter type to a delegate, which has a wider parameter type.
$ /usr/local/bin/mono contravariance.exe 
Proximity alert
Output.
This part of the C# tutorial we have talked about new features

Do you like this Tutorial? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

No comments:

FAQs | Privacy Policy | Contact | | Advertise | Donate