| By Bill Wagner | Article Rating: |
|
| November 16, 2007 12:00 PM EST | Reads: |
8,041 |
C# 3.0 represents a radical new approach to .NET development. The new language features were added primarily to support Language Integrated Query (LINQ), allowing you to query data using the same constructs regardless of where the data is currently stored. However, you'll find that there are many things you can do with these new features outside of queries. There's a learning curve for these new features, but by adopting them you'll find that you can be much more productive than you ever were in earlier versions of C#. In this article, I'll give you a whirlwind tour of C# 3.0 language features, and how you can leverage them in your work. After that I'll discuss the LINQ project and show you how these features are used to build LINQ. You'll see that they are simpler, more concise syntax for constructs you already use today. That will give you a big start up that learning curve.
Implicit Properties
The simplest feature is Implicit Properties. Implicit Properties are a shorthand notation to create a property with a backing field. In earlier versions of C#, you would write:
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
Now you can write this more concise version:
public string Name
{
get;
set;
}
You can combine implicit properties with property access modifiers to create read-only properties:
public string Name
{
get;
private set;
}
This syntax means that the class can modify the Name property, and all types can view the value of the Name property. The only drawback to implicit properties is that you can't access the compiler-generated field, even in your own class. In practice, that's rarely an issue because the JIT compiler will, in all likelihood, inline the Property Set Accessor and your type's code will directly access the compiler-generated backing field anyway.
The simplified syntax for implicit properties makes it easier for you to create classes that follow the recommended best practice of using properties instead of publicly accessible fields. Using an implicit property is no more work than creating a publicly accessible field. And, if you find that later you need more logic in your property accessors, you can add that in a way that is binary-compatible for your users. You replace the implicit property with an explicit property, add an explicit backing field, and add your custom logic. The public interface to your type remains the same. None of the clients of your component need any changes, or even a recompile.
Of course, there are some limitations to using the new implicit property syntax. Most notably, you can't enforce immutable types when you rely on implicit properties. The implicit backing field isn't read-only in the current type, and your private Set Accessor can be called from any method in the type.
Partial Methods
Next, we'll discuss partial methods. You'll likely only run into partial methods when you're extending wizard-generated code. Partial methods were added to provide for extensibility hooks in partial classes. It's best explained by example. Suppose I wrote the following generated class to protect a precious resource:
public partial class Smeagol
{
private readonly object myPrecious;
public Smeagol (object somePreciousResource)
{
myPrecious = somePreciousResource;
}
public object MyPrecious
{
get { return myPrecious; }
}
}
It's a partial class because developers may want to add functionality to the generated code. However, developers may also want to intercede and do something different when the precious resource gets accessed. I can add that capability using partial methods. First, I declare a partial method that can be called from the generated code:
partial void BeforeAccessingPrecious();
Next, I call the method wherever it's appropriate:
public object MyPrecious
{
get
{
BeforeAccessingPrecious();
return myPrecious;
}
}
Published November 16, 2007 Reads 8,041
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Bill Wagner
A frequent writer and speaker, Bill's work is published in ASP.NET Pro and Visual Studio Magazine. He is the author of C# Core Language Little Black Book and Effective C#. You can reach Bill at wwagner@srtsolutions.com.
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications



























