Holy Smokes!

Thursday, March 5, 2009

Just wasting everyone's time

It seems like I’ve been to busy or lazy to manage this blog lately.  Not intentional, just to lazy.  So I figured I would waist your time, and just share something meaningless.

Any of you have a ‘favorite’ new feature of C#? or .NET in general?  They pushed a fair amount into the last release.

Personally I think the smallest, least important language updates are my favorite ones.  Two of which:

Automatic Properties is one of them.  Take the following code.

public class TestClass : Object {   
private int m_intValue;

public int Value {
get {
return m_intValue;
}
set {
m_intValue = value;
}
}
}




using automatic properties, you could shorten that to this.








public class TestClass : Object {   
public int Value { get; set; }
}



The end result is a virtually identical IL.  So.. there is no performance hit, the code is cleaner and easier to read and maintain, and as an added bonus, it saves typing.



Another update I thoroughly enjoy is the new method of assigning values/references to object properties, upon initialization.  Take this example:




TestClass cls = new TestClass();   
cls.Value = 55;





You could replace that with this:



TestClass cls = new TestClass() { Value = 55 };





In that example, perhaps it doesn’t look like it’s doing much, and perhaps you’re right.  But you aren’t limited to assigning just one property at a time, you can assign as many as you like.  And it’s still just considered one line of code, even if you split it across multiple lines, so if you’re the type who doesn’t like to use braces excessively, this can really help keep the code trimmed down.




foreach (String strPath in Paths)    
trv.Nodes.Add( new TreeNode() {
Text = strPath,
Tag = new FileInfo(strPath)
});



Anyways, I just wanted to share.  What are you guys’ favorite new features?

2 comments:

  1. I'm never gonna get this blog thing right.
    Hold on while I try and figure this sucker out...

    ReplyDelete
  2. There, the code spacing is still a little messed, but I think we I got it...

    ReplyDelete

Followers