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



Component One


LearnNow
 


Component One

Reader rating:
Article source: CoDe (2008 Jan/Feb)


Article Pages:  1  2 - Next >


C# 3.0 Syntax Additions-Design Guidelines

C# 3.0 includes a few syntactical additions to the language. For the most part, Microsoft added these language additions to support Language Integrated Query (LINQ). These features include (but are not limited to) lambda expressions, extensions methods, anonymous types, implicitly typed local variables, automatic properties, and object initializers.

"
Extension methods have an innate ability to pollute your namespace with currently no way to scope them.
"

Most of the syntax additions fulfill very specific needs and should not reduce the importance of established coding and design methodologies and guidelines. When in doubt, prefer your established guidelines over the new syntax.

Microsoft’s Anson Horton has a great article on LINQ’s impact on the design of C# 3.0, The Evolution Of LINQ And Its Impact On The Design Of C# that goes into detail of these language features from a different perspective.

Lambda Expressions

You can think of lambda expressions as an evolution of C# 2.0’s anonymous methods and it is an attempt at bringing functional programming-a paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data-to C#.

Lambda expressions are based upon lambda calculus. See the References sidebar for more information.

PREFER methods over lambda expressions when the same code is used repeatedly.

PREFER using lambda expressions where anonymous delegates would have been appropriate in C# 2.0.

Extension Methods

Arguably one of the most controversial additions to C#, extension methods allow designers to “inject” static methods into any other class. Essentially extension methods are syntactic sugar for creating a static method that operates on the instance of a particular type. Previous to C# 3.0, you might write a general utility method like this:

public static bool IsStringPlural(String text,
    CultureInfo cultureInfo)
{/* ... */}

…and may be called as such:

String text = "languages";
Boolean isPlural = 
    StringExtensions.IsStringPlural(text,
        new CultureInfo("en"));

…and operates on a String object (ideally telling you that that word/phrase is plural for that given context). Extension methods allow you to associate a similar method to a class in a way that allows it to be called as if it were a member of that class. Creating an IsPlural extension method for the String class doesn’t change much from the original syntax of the IsStringPlural method declaration-it just adds a this keyword in the parameter list:

public static bool IsPlural(this String text,
    CultureInfo cultureInfo)
{/* ... */}

…and may be called as such:

String text = "languages";
Boolean isPlural = text.IsPlural(
    new CultureInfo("en"));

This method call syntax is arguably easier to read when used, but it reduces discoverability-there’s no way to tell from the line of code calling IsPlural that it really isn’t a member of String or what class the method really is declared in.

Extension methods compile to ordinary static methods, and can be used as such:

String text = "languages";
Boolean isPlural = StringExtensions.IsPlural(text,
    new CultureInfo("en"));

Extension methods are intended to be used in association with lambda expressions to provide vastly more readable and terser query expressions.

The main drawback of extension methods is resolution. Essentially all extension methods are global; each method with the same name and same argument count cannot currently be differentiated. Extension methods have an innate ability to pollute your namespace with currently no way to scope. This means, by simply adding a using statement to your file you can introduce compile errors.

DO use extension methods sparingly.

DO put extension methods in their own static class.

CONSIDER grouping extension methods that extend a particular class into a single static class and name that class “ClassNameExtensions”. If you do run into a name collision and are forced to use the static method call syntax, you don’t want to end up with reduced readability.

DO keep extension method classes in their own namespace to mitigate potential name collisions (if you run into a name collision you’re forced back to using a static method call).

Anonymous Types

Anonymous types allow you instantiate a class without having to declare it. This, of course, requires that you use the var keyword because you don’t have a name for your class. For example:

var person = new { Name = "Peter", Age=4};

As you might have inferred by the syntax, there’s no way to define a method on an anonymous type. This really restricts its usefulness in real-world scenarios. Since you’re not really declaring a type you can’t add attributes to the type; so, you can’t make it serializable.

Another feature of anonymous types is that they are immutable. This essentially means when you instantiate an anonymous type you’re not declaring fields, but properties; and those properties only have a get, not a set.

Anonymous types are really only useful for very short-lived data.

AVOID anonymous types for long-lived data.

&

By: Peter Ritchie

Peter has been working professionally in software development for over 17 years. He’s been working with computer software far longer; since his first computer, an Atari 800. Peter is president of Peter Ritchie Inc. Software Consulting Co, where he provides Windows-based software development services in Canada’s national capital region.

You can find Peter’s software-development-related pontification and advice at http://www.peterRitchie.com/blog/

Peter received the Microsoft MVP-Visual C# award (2006-2007) for his contributions to the online developer community and is a contributor to the Visual C# Developer Center (http://msdn2.microsoft.com/vcsharp).

code-magazine.com@peterRitchie.com

Fast Facts

The new C# 3.0 syntax offers some great new features. As with features of any language, use of this new syntax does not come without side-effects. These guidelines will help you to understand the new C# 3.0 syntax and help you avoid some of the pitfalls you can encounter with the new syntax.



Article Pages:  1  2 - Next Page: 'Implicitly Typed Local Variables' >>

Page 1: C# 3.0 Syntax Additions-Design Guidelines
Page 2: Implicitly Typed Local Variables

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.5 out of 5

13 people have rated this article.

rssbus

      Learn Now

 

Hacker Halted