Recommended

Skip Navigation LinksHome / Tips / View Tip

Tip: How to shorten your C# code?

+ Add to SilverlightShow Favorites
6 comments   /   posted by Denislav Savkov on Sep 17, 2008
(0 votes)
Categories: General

Some of the new features in C# 3.0 allow you to write shorter code that is easier to read and maintain. The following code couples have the same results.

  • Short syntax for object initialization initialization.

    C#

     

    ObjectType objectName = new ObjectType();
    objectName.PropertyName = newValue;
    objectName.PropertyName2 = newValue2;
     

     

    C#

    ObjectType objectName = new ObjectType(){ PropertyName = newValue, PropertyName2 = newValue2 };
  • Syntaxt for anonymous types

C#

SomeVeryLongNameOfType objectName = new SomeVeryLongNameOfType();

C#

var objectName = SomeVeryLongNameOfType();
  • Short syntax for collection initialization

C#

CollectionType collectionName = new CollectionType();
collectionName.Add( newItem1 );
..
collectionName.Add( newItemN );

C#

CollectionType collectionName = new CollectionType() { Item1,...,ItemN };

 

That's it!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to shorten your C# code?  

    posted by DDtMM on Sep 18, 2008 07:50

    I don't remember if it is in 3.0 or 3.5, but AutoProperties can save you a lot of time.

    Public string MyProp { get; set; }

    Also Linq can really shorten some login in a program.

  • RE: Tip: How to shorten your C# code?  

    posted by dejo on Sep 19, 2008 08:35

    Yes, Automatically implemented properties are also part of the 3.0 specification. Thanks for the reminder DDtMM.

  • RE: Tip: How to shorten your C# code?  

    posted by Rachida Dukes on Oct 06, 2008 09:20

    Very nice article about the C# 3.0.  I would like to know more about C# short cuts, can you direct me to the appropriate articles or documetation?

    Thanks,

    Rachida

  • RE: Tip: How to shorten your C# code?  

    posted by isit on Oct 17, 2008 04:33

    very nice but too brief .. thanks

  • RE: Tip: How to shorten your C# code?  

    posted by iiordanov on Oct 17, 2008 04:41

    Hi isit,

    Due to the big interest on this topic, we are going to write more detailed article about the ways you can shorten your C# code using shortcuts.

  • RE: Tip: How to shorten your C# code?  

    posted by iiordanov on Oct 17, 2008 04:53

    Hi All,

    Meanwhile, you can check out this article C# 3.0 Tutorial by Jonathan Worthington, there you can find a lot of useful things.
    Good luck!

Add Comment

 
 

   
  
  
   
Please add 6 and 3 and type the answer here:

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)