Concise C# Part 3 : C# 3 shortcuts

Posted on : 07-09-2009 | By : manitra | In : C#

0

The version 3 of C# brings us a lot of syntactic sugar to reduce the length of our code. Here are some of them.

This post is part of a serie about making C# code shorter.

Constructors with Property initializers

  1. //Verbose and Ugly
  2. Post value = new Post();
  3. value.Title = "New Version For C#";
  4. value.CreatedOn = DateTime.Now;
  5. CreatePost(value);
  6.  
  7. //Concise and Sweet
  8. CreatePost(new Post
  9. {
  10.     Title = "NewVersion For C#",
  11.     CreatedOn = DateTime.Now
  12. });

Collection initializers

  1. //Verbose and Ugly
  2. var names = new List<string>();
  3. names.Add("manitra");
  4. names.Add("yeah");
  5. SaveNames(names);
  6.  
  7. //Concise and Sweet
  8. SaveNames(new List<string> { "manitra", "yeah" });

Var keyword

  1. //Verbose and Ugly
  2. Dictionary<string, List<Post>> result = new Dictionary<string, List<Post>>();
  3.  
  4. //Concise and Sweet (But still trongly typed)
  5. var result = new Dictionary<string, List<Post>>();

Automatic properties

  1. //Verbose and Ugly
  2. public class Post
  3. {
  4.     private string title;
  5.     private string description;
  6.     private DateTime createdOn;
  7.  
  8.     public string Title
  9.     {
  10.         get
  11.         {
  12.             return this.title;
  13.         }
  14.         set
  15.         {
  16.             this.title = value;
  17.         }
  18.     }
  19.  
  20.     public string Description
  21.     {
  22.         get
  23.         {
  24.             return this.description;
  25.         }
  26.         set
  27.         {
  28.             this.description = value;
  29.         }
  30.     }
  31.  
  32.     public System.DateTime CreatedOn
  33.     {
  34.         get
  35.         {
  36.             return this.createdOn;
  37.         }
  38.         set
  39.         {
  40.             this.createdOn = value;
  41.         }
  42.     }
  43. }
  44.  
  45. //Concise and Sweet
  46. public class Post
  47. {
  48.     public string Title { get; set; }
  49.     public string Description { get; set; }
  50.     public DateTime CreatedOn { get; set; }
  51. }

Concise C# Part 2 : Try Catch and reusability

Posted on : 03-09-2009 | By : manitra | In : C#

0

Exception handling is an important part of any software. In this post, I will talk about a trick to make but some kind of reusability on exception handling.

This post is part of a serie about making C# code shorter

When you write code, you usually try to reuse your code by creating methods and calling those methods each time you the need their functionality. When working with exceptions, it’s a bit tricky because, they don’t follow the normal code flow, they jump out of your method … unless you catch them. The consequence is that you usually have multiple similar try {} catch {} blocks that are often copied and pasted.

This is what usually happens

  1. public void Method1()
  2. {
  3.     try
  4.     {
  5.         //method 1 content
  6.     }
  7.     catch (ThreadAbortException)
  8.     {
  9.     }
  10.     catch (IOException ex)
  11.     {
  12.         //IO exception handling
  13.     }
  14.     finally
  15.     {
  16.         //add any code for releasing ressource
  17.     }
  18. }
  19.  
  20. public void Method2()
  21. {
  22.     try
  23.     {
  24.         //method 2 content
  25.     }
  26.     catch (ThreadAbortException)
  27.     {
  28.     }
  29.     catch (IOException ex)
  30.     {
  31.         //IO exception handling
  32.     }
  33.     finally
  34.     {
  35.         //add any code for releasing ressource
  36.     }
  37. }

The solution to reuse the exception block

This is what you could do to reuse the same error handling code block in c# :

  1. public void Method1()
  2. {
  3.     Do(() =>
  4.     {
  5.         //method 1 content
  6.     });
  7. }
  8.  
  9. public void Method2()
  10. {
  11.     Do(() =>
  12.     {
  13.         //method 2 content
  14.     });
  15. }
  16.  
  17. protected void Do(Action method)
  18. {
  19.     try
  20.     {
  21.         //code to call before each methods
  22.         method();
  23.         //code to call after each methods success
  24.     }
  25.     catch (ThreadAbortException)
  26.     {
  27.     }
  28.     catch (IOException ex)
  29.     {
  30.         //IO exception handling
  31.     }
  32.     finally
  33.     {
  34.         //add any code for releasing ressource
  35.     }
  36. }