The Product C Type(c#1)

Below is how typically how a product class might have written in c#1.

class ProductInCSharp1
    {
        string name;
        public string Name { get { return name; } }

        decimal price;
        public decimal Price { get { return price; } }

        public ProductInCSharp1(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }

        public static ArrayList GetSampleProducts()
        {
            ArrayList list = new ArrayList();
            list.Add(new ProductInCSharp1("fsdfadad", 9.0m));
            list.Add(new ProductInCSharp1("fsdfadad", 9.0m));
            list.Add(new ProductInCSharp1("fsdfadad", 9.0m));
            list.Add(new ProductInCSharp1("fsdfadad", 9.0m));
            return list;
        }

        public override string ToString()
        {
            return string.Format("{0} : {1}", name, price);
        }
    }

Looking through the code, there are three limitations that could be demonstrated.

  • An ArrayList has no compile time information about what is it contains, my mistake we can even add string instead of new ProductClass, since there is no compile time validation.
  • Here we only have public getters, if we were to have setters to Name and Price, they too would have to be public
  • There is soooo much code for simple public getter/setter properties

Strongly Typed Collections in c# 2

C#2 addresses first and second problems mentioned above, most important change is generics

class ProductInCSharp2
 {
   string name;
   public string Name
   {
     get { return name; }
     set { name = value; }
   }

   decimal price;
   public decimal Price
   {
     get { return price; }
     set { price = value; }
   }

   public ProductInCSharp2(string name, decimal price)
   {
     Name = name;
     Price = price;
   }

   public static List<ProductInCSharp2> GetSampleProducts()
   {
     List list = new List<ProductInCSharp2>();
     list.Add(new ProductInCSharp2("West Side Story", 9.99m));
     list.Add(new ProductInCSharp2("Assassins", 14.99m));
     list.Add(new ProductInCSharp2("Frogs", 13.99m));
     list.Add(new ProductInCSharp2("Sweeney Todd", 10.99m));
     return list;
   }

   public override  string ToString()
   {
     return string.Format("{0}: {1}", name, price);
   }
 }

Following are the changes to be noted

  • We have now private setters, which can be set through constructor
  • List would now only accept of type ProductInCSharp2. i.e, now we have compile time validation

Automatically Implemented Properties In C# 3

class ProductInCSharp3
 {
   public string Name { get; private set; }
   public decimal Price { get; private set; }
 
   public ProductInCSharp3(string name, decimal price)
   {
     Name = name;
     Price = price;
   }
 
   public ProductInCSharp3()
   {
       
   }
 
   public static List<ProductInCSharp3> GetSampleProducts()
   {
     return new List
     {
           new ProductInCSharp3 { Name="West Side Story", Price = 9.99m },
           new ProductInCSharp3 { Name="Assassins", Price=14.99m },
           new ProductInCSharp3 { Name="Frogs", Price=13.99m },
           new ProductInCSharp3 { Name="Sweeney Todd", Price=10.99m}
     };
   }
 
   public override string ToString()
   {
     return string.Format("{0}: {1}", Name, Price);
   }
 }

Following are the changes to be noted here

  • Now Properties dont have any code associated with them
  • we are building hardcoded list in a different way.With no name and price variables to access, we are forced to use the properties everywhere in the class, improving consistency.

Named Arguments in c# 4

class ProductInCSharp4
  {
    readonly string name;
    public string Name { get { return name; } }
 
    readonly decimal price;
    public decimal Price { get { return price; } }
 
    public ProductInCSharp4(string name, decimal price)
    {
      this.name = name;
      this.price = price;
    }
 
    public static List<ProductInCSharp4> GetSampleProducts()
    {
 
      return new List<ProductInCSharp4>
      {
          new ProductInCSharp4(name:"Naruto", price:33.33m),
          new ProductInCSharp4name: "Assassins", price: 14.99m),
          new ProductInCSharp4name: "Frogs", price: 13.99m),
          new ProductInCSharp4name: "Sweeney Todd", price: 10.99m)
      };
 
    }
 
  }

though the benefits of specifying named Arguments is minimal here, but if a method or constructor has too many parameters , it can make meaning of the code much clearer.(for example if you were having a nullable parameter type and you are passing null, with out named parameters it would be difficult to know for which parameter you are passing null.)

Summarizing evolution of c#1 to c#4

  • c#1 – Readonly properties, and weakly typed collections
  • c#2 – Private property setters and Strongly typed collections
  • c#3 – Automatically implemented properties, Enhanced object collection and initialization
  • c#4 – Named Arguments for clearer constructor and method calls
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s