jump to navigation

c# 3.0 new language features – Auto implemented Properties June 23, 2008

Posted by fofo in C#, Visual Studio 2008.
Tags:
trackback

C# 3.0 comes with many new language features. Some of them are

  • Anonymous Types
  • Object Initializers
  • Collection Initializers
  • Lambda Expressions
  • Auto-Implemented Properties
  • Extension Methods
  • Partial Methods
  • Query Expressions
  • Expression Trees
  • Local Variable Type Inference

In this post i would like to explain the Auto implemented properties.

Whenever we need to create a new class with public properties we need to create private fields to store the values of these properties.

For example if i create a simple asp.net web application (c#) and call it “autoproperties” and then i add a new item-> a class file, called Customer, i will have a file in my solution “Customer.cs”

Inside the class body i can define my public properties and private fields. In my case i can have

 public class Customer
    {

        private string c_name;
       

        public int name
        {
            get { return c_name;}
            set { c_name = value; }
        }
    }

 

We have to use the same syntax for as many properties i intend to implement and use. In c# 3.0 we have an easier way to achieve this. Let’s try to use this new syntax which is easier to type and much more compact.

In our class example we need to have a second public property e.g surname

so in order to do that we just need to type in the class body

public string surname {get; set;}

There is no need to implement a private field to store the value. This new syntax does take care of this. If you are fan of the code snippets as i am, you can type in the class code editor

prop and hit tab twice you will see the following

public int MyProperty { get; set; }

You can change the data type and the name of the property as you wish.

If you want to have only a read only property you can type

propg and hit tab twice

public int MyProperty { get; private set; }

You can change the data type and the name of the property as you wish.

That is folks!!!! I will try to give examples of the new features of C# 3.0 in forthcoming posts.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl

Comments»

1. Object Oriented Programming Concepts with C#3.0 « DOT NET RULES - June 21, 2009

[…] C# 3.0 we had a new feature that is called Auto-implemented properties. Have a look here in one of my past posts to find out more about that.Basically with auto implemented properties,  […]

2. VB 10.0 new features and Visual Studio 2010 « DOT NET RULES - June 27, 2009

[…] properties, which is a feature that was available with C# 3.0, provides us with a way to specify a property of a class without […]

3. Rk - January 6, 2014

nice 1. I have found another basic example for C# 3.0 new feature, please have a look at http://www.etechpulse.com/2013/08/what-is-automatic-property-in-c-30-net.html

4. Johnk869 - May 2, 2014

I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again! ecdkfgeagcee

5. Johnd977 - May 2, 2014

I just like the helpful info you supply on your articles. I will bookmark your blog and take a look at once more here regularly. I’m somewhat sure Ill learn a lot of new stuff right right here! Best of luck for the following! cfdkbedcckde


Leave a comment