Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



Learn Now


rssbus
 


Xojo

Reader rating:
Click here to read 6 comments about this article.
Article source: CoDe (2004 - January/February)


Article Pages:  1  2 3 - Next >


Three Cool New Features in C#

C# has always had a reputation as a clean language with lots of innovation.The Whidbey-release of Visual Studio .NET ships with a new version of the C# compiler that has a number of great new features. Some of them will be implemented as generic runtime features that will show up in other languages as well, and some are truly C#-specific. All of them originated in the C# camp. In this article, I will shed some light on my three favorite new features.

1: Anonymous Methods

If you have developed in C#, you are probably familiar with Delegates. Delegates are objects that encapsulate references to functions. One of the most common uses of Delegates is the implementation of event handler code. To see this in action, create a new Windows Forms (WinForms) project, and drop a button onto your form. Then, double-click on the button to create code that handles the click event. Behind the scenes, the WinForms designer creates two separate pieces of code. There is the actual event handler code, which, after adding the messagebox call, might look like this:

private void button1_Click(
  object sender, System.EventArgs e)
{
  MessageBox.Show("Test");
}

Additionally, and usually hidden in the form's designer-generated code region, this code is wired up to the button's click event like so:

this.button1.Click += 
  new System.EventHandler(
  this.button1_Click);

Note that the event handler method has to conform to a certain signature (two parameters of type object and System.EventArgs in this case). This is defined in the System.EventHandler delegate.

The big question is: What did we gain by creating this separate method that is tied to the click event through the EventHandler delegate? Well, not a whole lot, because there is no true need to call the method otherwise. But all of this is necessary in order to make event handling work.

Anonymous Methods simplify this a bit. Rather than instantiating a delegate and creating a method to point to, the code for the whole handler method can be created inline. No method name is defined (hence the name Anonymous Methods), but parameters are still required. Here's what that syntax looks like:

this.button1.Click += 
  delegate(object sender, EventArgs e)
  {    MessageBox.Show("Test");   };

As you can see, what is assigned to the Click event is not a pointer to a method, but the entire method code itself including the parameters, and without a method-name. Note that there has to be a semi-colon at the end, because all of this is really just one line of code.

One of the really cool uses of this technique is the ability to pass code as a parameter. Envision a scenario where a delegate is used as a call-back, a pattern that is commonly used in a number of scenarios, such as asynchronous programming, or whenever a process reports on its progress. Here is an example that illustrates this:

public delegate void 
  Feedback(string Text);

public void Execute(Feedback d1)
{
  d1("Starting...");
  // More code
  d1("Still going...");
  // Mode code
  d1("Done.");
}

You could now instantiate a delegate that encapsulates a method with one string parameter and pass it to the Execute method to receive feedback on the method's progress. Alternatively, you could just call the method and pass the feedback code in as a parameter:

Execute(
  delegate(string s)
  {MessageBox.Show(s);} );

So everything within the parenthesis of the Execute() call is the anonymous method. And of course, you can pass in as many lines of code as you want. Oh, and before I forget, unlike manually created delegates, anonymous methods can actually see local variables defined in the calling method. So this works just fine:

string s2 = "Test";
Execute(
  delegate(string s)
  {MessageBox.Show(s+s2);} );
&

By: Markus Egger

Markus is the founder and publisher of CODE Magazine and EPS' President and Chief Software Architect. He is also a Microsoft RD (Regional Director) and the one of the longest (if not THE longest) running Microsoft MVPs (Most Valuable Professionals). Markus is also a renowned speaker and author.

Markus' spends most of his time writing production code. The projects Markus has worked on include efforts for some of the world's largest companies including many Fortune 500 companies. Markus has also worked as a contractor for Microsoft (including the Visual Studio team). Markus has presented at many industry events, ranging from local user groups to major events such as MS TechEd. Markus' written work has been published extensively and in magazine ranging from MSDN Magazine, to Visual Studio Magazine, and of course in Markus' own CODE Magazine and much more. Markus is a supporter of communities in North America, Europe, and sometimes even beyond.

Markus currently focuses on development in .NET (Windows, Web, Windows Phone, and WinRT) as well as Android and iOS. He is passionate about overall application architecture, SOA, user interfaces and general development productivity and building maintainable and reusable systems.

In his spare time, Markus is an avid windsurfer, scuba diver, ice hockey player and world traveler. On a rainy day, he is known to enjoy a good game on his PC or Xbox.

megger@eps-software.com

Fast Facts

Anonymous Methods, Generics, and Partial Types are among the new features that will enter the C# arena in the near future. Some have been long anticipated; others are a surprise. All of them increase productivity and code reuse.



Article Pages:  1  2 3 - Next Page: 'Partial Classes' >>

Page 1: Three Cool New Features in C#
Page 2: Partial Classes
Page 3: Generics

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.1 out of 5

52 people have rated this article.

rssbus

      Learn Now

 

RssBus