... and things that you still look forward to seeing in the upcoming versions.
This topic has essentially been covered here. It contains some very interesting suggestions indeed (a few of which are in fact noted to be planned for C# 4.0). Definitely worth reading through the whole thing.
Property-scoped fields
public int N {
private int n, m = 3;
get { return n + m; }
set { n = value; }
}
Chained null
checks
if (document!RootNode!ChildNode != null) ...
Multiple setters:
private Uri _linkUrl;
public Uri linkUrl
{
get { return _linkUrl; }
set
{
_linkUrl = value;
}
set<string>
{
_linkUrl = new Uri(value);
}
}
//this of course works fine
MyObj.linkUrl = new Uri("http://stackoverflow.com");
// but I could also just do this:
MyObj.linkUrl = "http://stackoverflow.com";
Conditional/filtered event handlers:
class A
{
public event EventHandler MyEvent;
}
class B
{
int x;
A classA = new A();
public ClassB()
{
classA.MyEvent += new EventHandler(OnMyEvent).Where(x > 5);
}
}
This would mean I only get events when the filter is matched. I could also envisage using other LINQ-style operators such as First() where you only get the event fired once and then never again.
For example, instead of writing:
private void OnMyEvent(object sender, EventArgs e)
{
if (x > 5)
{
// Do stuff.
}
}
I can specify the condition where the handler is attached to the event. The compiler would insert a new handler behind the scenes with the condition applied based on the method I define. This way, i can define a single handler that is used with different conditions on multiple events rather than needing one handler per event just because I need a different condition or filter in there. There wouldn't be a need for any new CLR support - this would just be syntax changes.
I want language support for exception filters as well.
Update
I believe my "filtered events" concept can be created through the use of .NET 4 and the Rx framework.
Nullable strings
string? strx;
strx.GetValueOrDefault(string.Empty);
Extension Properties
Same as extension methods but properties, would allow extending unowned code into fluent interfaces
objX.Is().Not.Something();
vs
objX.Is.Not.Something();
- Static extension methods and properties
- Built-in arithmetic support in Generics