Just thought I should share some thoughts and information about some of the new things that I think are a bit interesting. This is in no way a complete list of new things.
Language
Optional and named arguments
I feel VB6 coming back to me when I see this and I guess that this is something the VB.NET people already have? Optional arguments simply let’s you omit argument values and named arguments let’s you enter them in any order you’d like.
public int Test(int a, int b = 1, int c = 2, int d = 3) {
return a + b + c + d;
}
public string Hello(string name = "World") {
return "Hello, " + name + "!";
}
public void Main() {
Test(0); //Test(0,1,2,3)
Test(0, c: 5); //Test(0,1,5,3)
Test(d: 5, a: 0); //Test(0,1,2,5)
Hello(); //Hello("World");
}
I think this is something I will use for saving some lines of code where I earlier wrote several method/constructor overloads.
Note that both functions will work not only in methods but also in constructors and indexers.
Variance
Oh dear… Let’s see if I can make this one justice.
Everybody knows that you can write this:
//No problem!
string s = "Hello";
object o = s;
It works because string is a subclass of System.Object and you can always cast to a less derived object.
The following however does not work.
IList<string> strings = new List<string>();
IList<object> objects = strings;
// And if you think of if it kind of makes sense:
objects[0] = 1000.0;
var whatAmI = strings[0]; //Would contain a string representation of 1000.0
The thing is that the above code should be fine if the collection objects where “readonly”, say, IEnumerable<T> but in C# pre 4.0 you wouldn’t have any luck there either. But now!
//Will work in C# 4.0 since IEnumerable is a readonly collection
IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings;
Let’s try and make a “real” example. The following code will not compile in C# 3.0 but in 4.0 it’s party time. The reason is the last line of code. Only with C# 4.0 is it possible to cast apples and bananas to IEnumerable<Fruit>.
public class Fruit { }
public class Apple : Fruit { }
public class Banana : Fruit { }
public class Varicance
{
public static void Main()
{
//Bag of apples
var apples = new List<Apple>() {
new Apple(),
new Apple()
};
//Bag of bananas
var bananas = new List<Banana>() {
new Banana(),
new Banana()
};
//Let's put them together
IList<Fruit> fruitBasket = apples.AsEnumerable<Fruit>()
.Concat(bananas).ToList();
}
}
For a deeper explanation of variance I recommend watching the following video with Anders Hejlsberg. Expert to Expert: Erik Meijer and Anders Hejlsberg - The Future of C#
Dynamic
This is probably one of the biggest news in C# 4.0 and it’s also the one I will write least about. But this is what it is:
C# 4.0 introduces a new static type called dynamic and if you declare an object dynamic the properties and methods of that objects won’t be resolved until runtime. This is useful when communicating with other dynamic languages and COM objects.
Framework
Code Contracts
This is a way for you to more easily declare and visualize the contracts that a method must fulfill. Not only is it more easy for you as a developer to understand what the “rules” for a method is the compiler can also pre-check the conditions and find code that will break your contracts.
public object GetObject(string id)
{
Contract.Requires(!string.IsNullOrEmpty(id));
Contract.Ensures(Contract.Result<object>() != null);
return Repository.Get(id);
}
The contract will ensure that the id argument is not null or empty and also that the returned object is not null.
Parallel Extensions
Quite a lot of work has been done to add better multi core support to .NET, better as in easier to use. There is a improved ThreadPool, two new types: Task and Task<T> that you can think of as a lighter, easier to use Thread type and Parallel LINQ(PLINQ).
public void PLINQ()
{
//.AsParallel is all you need...
var data = GetData();
var q = from x in data.AsParallel()
where someFunc1(x)
orderby x.SomePropery
select x;
}
BigInteger
For when you need does reeeally big numbers… In theory the BigInteger has no maximum but eventually you will run out of memory.
//1e+100. One followed by one hundred zeros.
BigInteger googol = BigInteger.Parse("10000000000000000000000000" +
"0000000000000000000000000" +
"0000000000000000000000000" +
"0000000000000000000000000");
//(1e+100)^1000. That's one followed by 100 000 zeros!!
//(That is 27 pages in Word with nothing but zeros....)
BigInteger reallyBig = BigInteger.Pow(googol, 1000);
Better file system enumerations
The GetFiles and GetDirectories methods on System.IO.Directory and DirectoryInfo returns an array of objects. This is not always(when would it be?) ideal. A better solution would be if they returned IEnumerables and that the internally used yield return. And now they do! Or at least the new EnumerateXXXX methods do. This is far more efficient when writing code like this.
public FileInfo FindFile(string filename)
{
var directory = new DirectoryInfo(SOME_PATH);
foreach (var file in directory.EnumerateFiles())
if (file.Name.Equals(filename, StringComparison.InvariantCultureIgnoreCase))
return file;
}
There you go. Is I said, this is only a handful of the new things in C# and .NET 4.0 but it’s some of the coolest ;)
0412befd-e763-401e-8561-ce5ac6906559|1|5.0
C#, .NET
c# 4.0, .net 4.0