Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in C# 3.0? Part 2 : Page 5

The release of Visual Studio 2008 updates C# to version, 3.0, with several key language enhancements and LINQ support. Part Two of this series will walk you through C#'s new LINQ support features and other time-saving enhancements.


advertisement
Partial Methods
In C# 2.0, you dealt with the concept of partial classes, in which the definition of a class could be split into two. In C# 3.0, this concept is extended to methods: partial methods. To see how partial methods work, consider the following example.

Suppose you have a partial Contact class that contains two properties—Name and Email:

public partial class Contact { string _email; public string Name { get; set; } public string Email { get { return _email; } set { _email = value; } } }

Now, suppose you want to allow users of this partial class to optionally log the email address of each contact when its Email property is set. In this case, you can define a partial method called LogEmail(), as shown in Listing 1.



As you can see in Listing 1, you've defined a partial method named LogEmail() that gets called when a contact's email is set via the Email property. Note that this method has no implementation. So where is LogEmail()'s implementation? It can optionally be implemented in another partial class. For example, if another developer decides to use the Contact partial class, (s)he can define another partial class containing the implementation for the LogEmail() method:

public partial class Contact { partial void LogEmail() { //---code to send email to contact--- Console.WriteLine("Email set: {0}", _email); } }

And so now, when you instantiate an instance of the Contact class, you can set its Email property as follows and a line will be printed in the output window:

Contact contact1 = new Contact(); contact1.Email = "weimenglee@learn2develop.net";

But what if there's no implementation of the LogEmail() method? In that case, the compiler simply removes the call to this method and, thus, there is no change to your code.

Partial methods are useful when you are dealing with generated code. For example, suppose the Contact class is generated by a code generator. The signature of the partial method is defined in the class, but the implementation is totally up to you to decide if you need to.

Partial methods must adhere to the following rules:

  • They must begin with the partial keyword and the method must return void.
  • They can have ref parameters but not out parameters.
  • They are implicitly private and, therefore, cannot be virtual.
  • They cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • They can have static and unsafe modifiers.
  • They can be generic; constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing declaration.
  • Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining declaration.
  • They cannot make a delegate to a partial method.
Up to Speed
Now that you've learned about all the new features available in C# 3.0, I hope they can help save you time and hassle in your next project.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap