One of the things I can't say that I've enjoyed doing in the past when writing .NET classes was creating properties and associated fields. I leveraged the "prop" code snippet a lot in Visual Studio 2005 so that I didn't have to type much, but I still had to go through and define the field name and property name in the code. While public fields could certainly be used in some cases, they don't provide any control over the data being assigned (especially if the field is a string type) and aren't supported well when it comes to data binding. As a result I always create public properties to wrap fields even if I don't have any "extra" logic that needs added into the get or set blocks. Here's the standard way of creating fields and properties using C# 2.0:
public class Person
{
string _FirstName;
string _LastName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
With C# 3.0 a new feature called "automatic properties" is now available. When you use the "prop" code snippet in Visual Studio 2008 you'll see that empty get and set blocks are added and that no associated field is added into the code. The compiler will automatically generate the backing field at compile time if it finds empty get or set blocks saving you the work. You can still add get and set blocks that have additional filtering logic in them as well although you'll have to type all of that yourself of course.
Here's an example of how properties can be written in C# 3.0....you gotta love it especially when you compare how much more compact this technique is compared to the older way of doing things.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
New C# "Orcas" Language Features: Automatic Properties, Object ...
ASP.NET, Visual Studio, ASP.NET 2.0, .NET ... Scott Guthrie lives in Seattle and builds a few products for Microsoft
more ...
go to website
Cached
C# 3.0 Features: Automatic Properties - Dan Wahlin's WebLog
ASP.NET, AJAX, Silverlight, XML, and Web Services Exploration ... Object Initializers; Automatic Properties Anonymous Types Extension Methods Lambda ...
more ...
go to website
Cached
ASP.NET QuickStart Tutorials
User Profiles - The profile feature in ASP.NET 2.0 allows ... A user's profile is a collection of properties that define ... Controlling the Automatic Save Behavior of the Profile
more ...
go to website
Cached
ASP.NET QuickStart Tutorials
... Declarative resource expressions to tie controls or their properties to ... ASP.NET v2.0 provides automatic support for resource (RESX or RESOURCE) files that follow a specific ...
more ...
go to website
Cached
C# 3.0 Automatic Properties explained - B# .NET Blog
Notice that automatic properties should have both a getter and a setter declared. ... Thursday, March 29, 2007 4:20 AM by ASP.NET
more ...
go to website
Cached
why not just use fields instead of properties? Well - there are a lot of downsides to exposing public fields. Two of the big problems are: 1) you can't easily databind against fields, and 2) if you expose public fields from your classes you can't later change them to properties (for example: to add validation logic to the setters) without recompiling any assemblies compiled against the old class.
The new C# compiler that ships in "Orcas" provides an elegant way to make your code more concise while still retaining the flexibility of properties using a new language feature called "automatic properties". Automatic properties allow you to avoid having to manually declare a private field and write the get/set logic -- instead the compiler can automate creating the private field and the default get/set operations for you.
For example, using automatic properties I can now re-write the code above to just be:
public class Person {
public string FirstName {
get; set;
}
public string LastName {
get; set;
}
public int Age {
get; set;
}
}
Or If I want to be really terse, I can collapse the whitespace even further like so:
public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
When the C# "Orcas" compiler encounters an empty get/set property implementation like above, it will now automatically generate a private field for you within your class, and implement a public getter and setter property implementation to it. The benefit of this is that from a type-contract perspective, the class looks exactly like it did with our first (more verbose) implementation above. This means that -- unlike public fields -- I can in the future add validation logic within my property setter implementation without having to change any external component that references my class.
Videos from YouTube
Title: ASP.NET 2.0 Nested GridView
Categories: Screencast,Simple,Gridview,2.0,Example,Howto,Nested,Sample,ASP.NET,
Published on: 2/22/2007 10:55:45 AM
Title: Data Access in the ASP.NET 2.0 Framework
Categories: 2.0,Walther,Data,Framework,Stephen,Howto,the,Access,Windows,Programming,in,ASP.NET,
Published on: 10/25/2007 7:32:57 PM
Title: ASP.Net (VB.Net) Session Variable ArrayList - Shalvin
Categories: Tech,VB.Net,Shalvin,.Net,Session,ArrayList,Microsoft,Variable,
Published on: 2/2/2008 10:58:58 PM
Title: asp asp.net programacion tutorial master pages c#
Categories: c#,video,pages,asp.net,Film,master,tutorial,asp,programacion,
Published on: 11/3/2006 8:19:39 AM
Title: AJAX Y ASP.NET
Categories: Howto,AJAX,ASP.NET,
Published on: 5/26/2007 2:39:34 PM
Thursday, September 18, 2008
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment