Geeks With Blogs
Saqib Ullah BootStrapper Know How
This is my C# class and its compatible with all .Net framework versions.
 public class Person
{
    // Fields
    private string _name;
    private int    _age;
    private string _city;
    private string _company;
 
    // Properties
    public string Name
    {
       get { return _name; }
       set { _name = value; }
    }
    public int Age
    {
       get { return _age; }
       set { _age = value; }
    }
    public string City
    {
       get { return _city; }
       set { _city = value; }
    }
    public string Company
    {
      get { return _company; }
      set { _company = value; }
    }
}
From the last bit of class code every thing is old so what is new.
 
Here is new technique in C# 3.0 to define properties in the terse and brief syntax like this,
Person  objPerson new Person {Name=”Saqib Ullah”, City=”Karachi”, Company=”InboxBiz”, Age=26}
 
I hope this post increase your C# 3.0 little bit.
 
Posted on Sunday, April 22, 2007 9:09 AM OOPs , .Net | Back to top


Comments on this post: Never Before

# re: Never Before
Requesting Gravatar...
What was the point of doing this, besides maybe curbing those developers that get it in their head to write a constructor for every single possible way to initialize the object?

Here are two ways of doing the exact same thing:

Person person = new Person {Name=”Saqib Ullah”, City=”Karachi”, Company=”InboxBiz”, Age=26};

or

Person person = new Person();
person.Name=”Saqib Ullah”;
person.City=”Karachi”;
person.Company=”InboxBiz”;
person.Age = 26;


On second thought, it does appear the the new initializer is cleaner.
Left by Shadowin on Apr 22, 2007 5:52 PM

# re: Never Before
Requesting Gravatar...
This is one good way of initialization. Dint know it could be done this way. Thanks for the post
Left by Varun on Nov 13, 2009 12:07 AM

# re: Never Before
Requesting Gravatar...
AN EQUAL SIGN IS MISSING HERE:
Here is new technique in C# 3.0 to define properties in the terse and brief syntax like this,
Person objPerson new Person {Name=”Saqib Ullah”, City=”Karachi”, Company=”InboxBiz”, Age=26}
Left by eduardo belmonte on Jan 15, 2010 9:12 AM

Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net