Three C# 2.0/3.0 Syntaxes That You Didn’t Know But Were Afraid to Ask
Working with other colleagues, I found these C# syntaxes are still not well-known and used, so I thought of blogging on them.
1 – Properties Without Members
In the old days, before C# 3.0, we used to write syntax like:
public class Point {
private int _x;
private int _y;
public int X {
get {
return _x;
}
set {
_x = value;
}
}
public int Y {
get {
return _y;
}
set {
_y = value;
}
}
}
But if you are not doing any special processing in your property, you can use a shorter syntax introduced in C# 3.0:
public class Point {
public int X {
get;
set;
}
public int Y {
get;
set;
}
}
2 – The ‘??‘
While all of use, especially those coming from C/C++ background have used the ternary operator ‘?:‘, such as:
Point point1 = null;
// some code to initialise the point1...
Point point2 = (point1 == null ? new Point() : point1);
However, C# 2.0 introduced this new syntax:
Point point1 = null;
// some code to initialise the point1...
Point point2 = (point1 ?? new Point());
Which does exactly the same as the previous syntax.
3 – Initialising Properties when Creating an Object
Point point = new Point();
point.X = 1;
point.Y = 1;
Or you can use the C# 3.0 syntax:
Point point = new Point() { X = 1, Y = 1};
Probably if you are using LINQ then you have used this code several times.
14Comments
leave your ownNice.
Just as a side note:
here is how initialize a reference if it is null
Point point = (point ?? new Point());
I also like Anonymous Delegates available starting .NET 2.0
One comment on your property initialization example: you can also omit the “()” from a parameterless constructor, such as:
var point = new Point { X = 1, Y = 1 };
#1 and #3 are .net 3.5, not 3.0.
Anyone working with C# 3.0 for several months should know about these. Pick up any C# 3.0 book and these are covered in the first couple chapters (i.e. John Skeet’s book).
To SR: I believe that the framework is .NET 3.5, but the corresponding version of C# is 3.0.
Bart Czernicki,
I agree. However, not all developers had the chance of reading a book explaining the new additions.
‘??’ is the coalesce operator.
Here’s #1.5: Even after having used auto-properties for a while, I didn’t realize that these were valid:
public IList<MyType> PropName {get; private set;}
public IList<MyType> PropName2 {get; protected set;}
This is great, because it lets you set the value in a constructor, etc., not expose the setter publicly, and still use auto-properties.
To Mathias:
The host says C# 3.0, not .NET 3.0
The version of .NET is a little confusing.
C# 2.0 go along with .NET 2.0;
Then, MS release .NET 3.0 just with 4 additional libraries WCF, WPF, WF and CardSpace. This is really a version.
C# 3.0 go along with .NET 3.5.
I believe MS ust 3.5 instead of 4.0 just to align C# version and .NET version in future.
If I didn’t know about these syntices (yes, I said ‘syntices’), then how could I be afraid to ask about them? Doesn’t quite make sense.
Anyways, #1 I’ve known about, and is pretty popular in all of the “What’s new in C# 3.0?”
#3 is some what popular, but not as popular as #1, and #2, I don’t think I’ve seen that one before.
Carl,
Thank you for the correct pronunciation, however, when searching for both words in Google.co.uk :
- “syntices” : returns 203 results
- “syntaxes” : returns 418,000 results <– More popular
So I am going with the flow even though the first one might be correct, because I would be understood by more audience.
Honestly, I have researched this word before writing the title.
- Some people are afraid from the unknown, so you might be afraid to ask
- This is popular, however, I knew developers who are working on .NET 3.5 but not using them and I am closing the hole.
- I like the web 2.0 logo on your site
Adam,
I was trying to look smart by using ‘syntices’ instead of ‘syntaxes’. Both are correct.
Still unclear about asking about something you don’t know, but maybe that’s just me.
Keep up the good work with the blog.
If you’re interested in condensing code (which we all should be interested in) and you’re also (apparently) interested in ASP.NET WebControls (ref; your validator blogs) you should have a look at this; http://ra-ajax.org/jquery-ish-selector-for-webcontrols.blog – which is my latest blog
Thank you for the correct pronunciation, however, when searching for both words in Google.co.uk.
I was trying to look smart by using ‘syntices’ instead of ‘syntaxes’. Both are correct.
Leave a Reply