Thursday, 3 July 2014

New 5 Jems in C# 5.0



Binding Operators

Binding Operators may be C# 5.0. The support for Binding on .NET objects is awesome. I came to know this from Chris on his post where he speaks how Binding operators looked like long ago. The Delphi assignment operator := is now being used for this purpose. :=: would be used for two way binding. Thus:

comboBox1.Text :=: textBox1.Text; 
 means there would be two way binding between the properties so that when ComboBox1.Text changes its Text, it will automatically change the value of Textbox1.Text. Binding is actually not a new thing to the system. In WPF, we already have Binding in place, but for that we need the properties to have implemented from INotifyPropertyChanged. So it is not in the language, the WPF objects are doing this internally by handling the PropertyChanged event on the object. But if that is included as an operator, it would be a cool feature to add on.

Support for Dynamic to the Compiler

Another important candidate for C# 5.0 would be the support for dynamic objects to the compiler itself. There are quite a few occasions where we already did face the flavor of Dynamic behavior of object. You can now defer the determination of type for the object to runtime using the keyword dynamic which is a compile time dynamic while runtime static. The introduction of Expression Trees also defers the compilation of an expression at runtime. You can create objects dynamically at runtime using either inheriting from DynamicObject (which is very easy to do) or by using CodeDom to compile a code at runtime and run the code using reflection. But what if these things are supported by the compiler itself.
As pointed out by Magnus in his post, The future of C# 4.0 and then 5.0, he clearly pointed out what the next version of C# would add up. The compiler must be supporting quite a number of APIs which lets you tweak your code to generate and compile the code at runtime.
But these are not the end of C# 5.0. People all around the globe are putting efforts to make a Wishlist on C# 5.0. A few of those might be:
Pavel's Wishlist on C# 5.0 where he specified quite a number of interesting specs on C# 5.0 but will it be all.
Probably if you ask me what would be your own language features which you like to see, I may name quite a few which I miss:

1. Support for parametrized constructors in Generics.

Yes. it would be nice to have this option in. As we have:

public class T MyClass : T: class, new()

//we might have 
public class T MyClass : T:class, new(int)

This might need adjustment to the compiler. I don't know if it is possible or not. But I definitely miss this in C#.

2. Support for WeakDelegate or WeakEvents

WeakReference, as I have discussed already in my blog or you come across it somewhere else, gives you a tweak to GC. It lets you refer to an object weakly while the GC may still collect it in the process. Thomas, rightly identified that WeakDelegate does not work now. You cannot have WeakDelegate defined in your scope.

3. Better treatment for Null

Working with Null has always been a problem for me. We have to treat nulls separately because there is no type associated with Nulls. With the introduction of Nullables around or Null coalesce operators, still there is scope of improving the Null treatment in the language. Say for instance,

int x? = null;
int y? = x + 40;
It was introduced by Earlz in his post. Really looks nice, huh.

4. Smart Case support

Yes, Switch - Case could allow expressions. While working with it, I miss it very often. Just like what Charles pointed out, I agree with him totally. I would like to have the option to specify:

   case string.IsNullorEmpty(myotherobj):
   case myotherobj.Trim().Lower:

5. Extension properties

Just as we support extension methods, we could also have extension properties which could probably be used from Extension methods only. It is nice to have in certain cases.

public static int MyExtensionProperty { get;set;}
Something like this might come in very handy at times. Probably some other syntax than Attribute Associate would be more fruitful.
Finally, I would like to see your feedback on the topic. Any other things which you miss? You can point them out to me.
Thanks for reading.


No comments :