# Friday, 11 September 2009

Back To Basics

Automatic properties were introduced in C# 3.0 as a convenience to developers.

C# and Visual Basic .Net provide two ways for a class to maintain state: Fields and Properties.

A field is a simply a public variable defined inside a class, such as in the following example

public string foo;

A property looks much like a field to the outside world, but exposes two methods – a “getter” to allow you to retrieve the value of the property and a “setter” to allow you to assign a value to the property. The following sample demonstrates how to implement a read-write property in C#.

private string _firstName;
public string FirstName
{
    get
    {
        return _firstName;
    }
    set
    {
        _firstName = value;
    }
}

This is a common use of property getters and setters – to assign and retrieve the value from a private variable. Because there is no extra logic in these methods, similar functionality could be achieved using a field, but I prefer to use properties in case I decide later to add some validations or calculations to the setter or getter. Making a change like that to a property will not break the class signature, forcing a recompile of dependent objects.

But this is a lot of typing for such simple functionality. So much in fact that Visual Studio 2005 provided a right-click accelerator to type much of it for you.

Beginning with C# 3.0, automatic properties provide a shorthand notation for this functionality.  Simply provide the scope, the type and the name of the property, followed by “{get; set; }” and the framework will automatically create a private variable for you and provide code to save and retrieve to/from that private variable. This is all done behind the scenes for us so we don’t need to write any of this code.  Below is a sample of the code we need to write for a public property.

public string FirstName { get; set; }

To make a property read-only, simply omit the "set" keyword. To make it write-only, omit the "get" keyword. Below is syntax for the same property if I wanted it to be read-only.

public string FirstName { get; }

The only disadvantage to automatic properties is that the rest of the code in our class no longer has access to the private variables create by the framework. Even from within the same class, we must go through the public property in order to get or set the private variable.

This is a small price to pay for the amount of code we are saving.

Automatic properties not only require less typing, they are far easier to read when your class has a bunch of properties. Which of the following easier to read?

public string FirstName { get; set; }
public string LastName { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }

or

private string _firstName;
public string FirstName
{
    get
    {
        return _firstName;
    }
    set
    {
        _firstName = value;
    }
}

private string _lastName;
public string LastName
{
    get
    {
        return _lastName;
    }
    set
    {
        _lastName = value;
    }
}

private string _streetAddress;
public string StreetAddress
{
    get
    {
        return _streetAddress;
    }
    set
    {
        _streetAddress = value;
    }
}

private string _city;
public string City
{
    get
    {
        return _city;
    }
    set
    {
        _city = value;
    }
}

private string _state;
public string State
{
    get
    {
        return _state;
    }
    set
    {
        _state = value;
    }
}

private string _zip;
public string Zip
{
    get
    {
        return _zip;
    }
    set
    {
        _zip = value;
    }
}

I think you’ll agree that it is far easier to read and understand the more terse syntax of the first example.  And code that is easier to understand is easier to maintain.

Because both syntax versions compile to the same Intermediate Language, I recommend always using the newer Automatic Properties syntax when you create properties that do nothing more than save and retrieve state.

Download the following sample code to see automatic properties in action: DemoAutomaticProperties.zip (26.83 KB)
Friday, 03 May 2013 18:26:07 (GMT Daylight Time, UTC+01:00)
You should rerutn a super class or interface for both objects you could rerutn. If you havenb4t defined a super class for them, you can still rerutn Object and do the not so good instanceof validation to know which class has been rerutned.Code Example:class SuperClass {}class MyClass1 extends SuperClass {}class MyClass2 extends SuperClass {}private SuperClass myMethod(String parameter) { SuperClass sc = null; //just trying some code, this could be replaced for your logic if (String.equals( 1 )) { sc = new MyClass1(); } else { sc = new MyClass2(); } rerutn sc;}private void myOtherMethod(String parameter) { //some code here SuperClass sc = myMethod(parameter); if (sc != null) { //securing the upcast if (sc instanceof MyClass1) { //logic of MyClass1 instance } if (sc instanceof MyClass2) { //logic of MyClass2 instance } } //more logic over here } +2Was this answer helpful?
Friday, 10 May 2013 12:39:33 (GMT Daylight Time, UTC+01:00)
<url>http://www.getbesteddeals.com/|kamagra viagra cialis apcalis</url> 9907 <url>http://www.lookingforedmeds.com/|cialis</url> gmlk
Sunday, 12 May 2013 12:07:25 (GMT Daylight Time, UTC+01:00)
<url>http://www.edprices4u.com/|how does viagra work</url> =[ <url>http://www.lookingforedmeds.com/|discount cialis</url> 761
Sunday, 12 May 2013 12:07:27 (GMT Daylight Time, UTC+01:00)
<url>http://www.edmedsoptions.com/|2003 cialis levitra market sales viagra</url> 006788 <url>http://www.edprices4u.com/|viagra</url> 6595
Sunday, 12 May 2013 12:07:30 (GMT Daylight Time, UTC+01:00)
<url>http://www.edprices4u.com/|over the counter viagra</url> 661 <url>http://shoppingforautoinsurance.net/|auto insurance quotes</url> >:DDD
Monday, 20 May 2013 16:16:35 (GMT Daylight Time, UTC+01:00)
<url>http://www.mymedspricelist.com/|valtrex pills to buy</url> 113 <url>http://www.autoprotectionoptions.com/|car insurance rates</url> 357371
Wednesday, 22 May 2013 16:22:00 (GMT Daylight Time, UTC+01:00)
<url>http://www.findlifeinsurquotes.com/|low cost life insurance</url> 8((( <url>http://www.getcheapautoinsur.com/|free car insurance quotes</url> %-]]
Friday, 24 May 2013 13:31:28 (GMT Daylight Time, UTC+01:00)
<url>http://www.autoprotectionoptions.com/|cheap car insurance</url> 4168 <url>http://www.findlifeinsurquotes.com/|permanent life insurance</url> zhiwt
Sunday, 26 May 2013 16:09:37 (GMT Daylight Time, UTC+01:00)
<url>http://www.autoprotectionoptions.com/|auto insurance quotes</url> yogfrj <url>http://www.getcheapautoinsur.com/|car insurence</url> %))
Tuesday, 28 May 2013 11:20:25 (GMT Daylight Time, UTC+01:00)
<url>http://www.findedmedshop.com/|sildenafil</url> ylht <url>http://www.bestpillformenonline.com/|buy cialis generic</url> =OOO
Tuesday, 28 May 2013 11:20:27 (GMT Daylight Time, UTC+01:00)
<url>http://www.bestwaytotreated.net/|where to buy viagra</url> nbcmfi <url>http://www.locateinsurancedeals.com/|car insurance quotes</url> frst
Thursday, 30 May 2013 09:00:47 (GMT Daylight Time, UTC+01:00)
<url>http://www.findedmedshop.com/|sildenafil</url> dsfbmi <url>http://www.locateinsurancedeals.com/|car insurance quotes</url> :OOO
Saturday, 01 June 2013 12:10:02 (GMT Daylight Time, UTC+01:00)
<url>http://www.edpillspricechart.com/|buy cialis</url> 126 <url>http://www.edmedsshopping.com/|viagra</url> fhuzbx
Monday, 03 June 2013 08:28:55 (GMT Daylight Time, UTC+01:00)
<url>http://www.bestwaytotreated.net/|buy viagra online</url> uked <url>http://www.edmedsshopping.com/|viagra cheap</url> 880
Monday, 03 June 2013 08:28:59 (GMT Daylight Time, UTC+01:00)
<url>http://www.bestwaytotreated.net/|buy viagra online</url> 67667 <url>http://www.edmedsshopping.com/|kamagra viagra cialis apcalis</url> 35146
Tuesday, 25 June 2013 12:44:45 (GMT Daylight Time, UTC+01:00)
<url>http://www.freequotesguide.com/|nj car insurance quotes</url> hgu <url>http://www.vadisrad.com/|college online anthem</url> 90069 <url>http://www.topallergytreatment.com/|purchase prednisone</url> uasea <url>http://www.insurcompanieslist.com/|country insurance auto</url> 7267 <url>http://www.improveyourerection.com/|sildenafil</url> %-]
Tuesday, 25 June 2013 12:44:50 (GMT Daylight Time, UTC+01:00)
<url>http://autoinsurancechoice.net/|auto insurance quotes</url> hfkvlr <url>http://www.getyourerectionsback.com/|generic cialis</url> >:) <url>http://www.insureyourselfcheap.com/|auto insurance online</url> 77039 <url>http://www.edmedspartners.com/|buy cialis online</url> 476583 <url>http://www.edremedies.net/|cialis online</url> 27420 <url>http://www.yourcarinsurancequotes.net/|car insurance quotes</url> >:DD
Sunday, 30 June 2013 14:07:48 (GMT Daylight Time, UTC+01:00)
<url>http://www.usaglobalquotes.com/auto-insurance-quotes-south-carolina.html|auto insurance quotes SC</url> ljxni <url>http://www.mycheapestinsurer.com/|teenagers auto insurance</url> 7830 <url>http://www.obs-dfw.org/|online university degrees</url> =O <url>http://edtreatmentoffers.net/|buy cialis</url> psmf <url>http://www.anystatecarinsurance.com/cheap-car-insurance-IL.html|Chicago cheap car insurance</url> 3665 <url>http://www.goldencoathanger.com/|recreation online degree</url> :] <url>http://www.yourcarinsurancequotes.net/missouri-car-insurance-quotes.html|car insurance quotes MO</url> 738
Thursday, 04 July 2013 20:19:44 (GMT Daylight Time, UTC+01:00)
<url>http://www.mycheapestinsurer.com/|cheapest car insurance</url> 523 <url>http://www.zumurphy.com/|online buy viagra</url> =[ <url>http://edtreatmentoffers.net/|cialis on sale</url> 55655 <url>http://www.tomoshibata.org/|cialis best price usa</url> 62531 <url>http://www.fabulousmuses.com/|caffeine impotence</url> %DD <url>http://www.soablondewalksintoareview.com/|exercise impotence</url> gyf
Saturday, 06 July 2013 15:27:02 (GMT Daylight Time, UTC+01:00)
<url>http://www.carinsurancechoice.net/|NJ car insurance</url> :D <url>http://www.aboutcarinsurancerates.com/|car insurance quote</url> 693942 <url>http://www.getmedicationonline.com/|Brand Name Viagra Online</url> :( <url>http://www.edproductsonline.com/|Buy Viagra online no prescription needed</url> tzub <url>http://www.geterection.net/|cheapest generic cialis no prescription</url> ghs
Monday, 08 July 2013 01:00:46 (GMT Daylight Time, UTC+01:00)
<url>http://www.snoco150.net/|buy levitra</url> trnu <url>http://www.freequotesguide.com/|insurance car quotes</url> 896498 <url>http://www.buyedmedsonline.net/|Buy Sildenafil</url> 8)) <url>http://www.cheapinsurersinyourstate.com/|free car insurance</url> mwkjkk <url>http://insuredcar.net/|car insurance quotes</url> prtpgc <url>http://www.soablondewalksintoareview.com/|viagra online</url> %D
Thursday, 11 July 2013 03:53:42 (GMT Daylight Time, UTC+01:00)
<url>http://autoinsurancechoice.net/|free quotes auto insurance</url> 8((( <url>http://edtabletsonline.net/|viagra uten resept</url> lmydj <url>http://edmedicationguide.net/|low cost viagra generic</url> gvo <url>http://www.getmedicationonline.com/|viagra online without prescription</url> 8OO <url>http://getmedonline.net/|buying viagra on line</url> =OOO <url>http://www.cheapinsurersinyourstate.com/|CHEAPEST CAR INSURERE</url> 2617 <url>http://autoinsuranceguidance.net/|public liability insurance market</url> 5304
Monday, 15 July 2013 07:16:30 (GMT Daylight Time, UTC+01:00)
<url>http://www.mainevillemotors.com/|"auto insurance quotes"</url> omqbh <url>http://www.onlinecollegeoffers.net/|accredited colleges online</url> %PPP <url>http://dallasinsuranceonline.com/|TX car insurance quotes</url> 8-[ <url>http://www.getmedicationonline.com/|viagra levitra</url> 43464 <url>http://www.erectionperformance.net/|online cialis</url> 8((
Friday, 19 July 2013 13:34:27 (GMT Daylight Time, UTC+01:00)
<url>http://www.buyautocoveronline.com/cheap-auto-insurance-NC.html|buy cheap state farm car insurance Charlotte</url> 00952 <url>http://www.carinsurtexas.com|average car insurance rates</url> >:-]] <url>http://www.texasautoinsur.com|insurance online cheap holiday texas</url> 063 <url>http://choosecarinsurance.com/|car insurance qoutes</url> 071928 <url>http://www.bestautoinsurancepolicies.net/|car insurance in florida</url> 725 <url>http://insuredcar.net/|liability insurance worker</url> :-PPP
Monday, 22 July 2013 11:09:42 (GMT Daylight Time, UTC+01:00)
<url>http://insuranceinhouston.net/|quotes on car insurance TX</url> 8-OO <url>http://bestcollegeonline.net/|accredited online universities and colleges</url> 6668 <url>http://getcheapinsurancehere.com/|old car insurance for</url> =-O <url>http://www.comparequotesonline.net/|car insurance quotes</url> ahobpl <url>http://edtreatmentoffers.net/|cheap cialis prices</url> 9801 <url>http://www.carinsurcalifornia.com|car insurance</url> cbx
Wednesday, 24 July 2013 07:00:35 (GMT Daylight Time, UTC+01:00)
<url>http://carinsuranceinmemphis.net/|car cheap insurance young Memphis</url> >:DDD <url>http://www.texascarinsur.com|free car insurance quote canada</url> fig <url>http://edmedicationguide.net/|sildenafil online</url> phunz <url>http://www.buyautocoveronline.com/|quotes car insurance pa</url> 484373 <url>http://choosecarinsurance.com/|cheap car insurance</url> %(((
Saturday, 27 July 2013 18:03:35 (GMT Daylight Time, UTC+01:00)
<url>http://www.qualityskincare.net/|accutane on line prescription</url> oqueor <url>http://www.topcarprotection.com/|cheap car insurance in florida online</url> nvy <url>http://www.insurancequoteslist.net/|cheap auto insurance</url> 893982 <url>http://www.mycarinsuranceprovider.com/|cheap car insurance</url> 6891 <url>http://www.getacheapcoverage.com/|car insurance quotes</url> 6150 <url>http://www.insurcompanieslist.com/|cheap auto insurance</url> 45570
Tuesday, 30 July 2013 08:58:43 (GMT Daylight Time, UTC+01:00)
<url>http://www.getafirmeerection.com/|buy viagra online</url> %(( <url>http://findyourdrugstore.net/|viagra no prescription</url> 17982 <url>http://www.strongererection.net/|Where to Buy Cialis on Line</url> xcara <url>http://www.onlineedrugs.com/|viagra buy online</url> 8)
Friday, 02 August 2013 02:51:20 (GMT Daylight Time, UTC+01:00)
<url>http://findyourdrugstore.net/|viagra om line</url> =P <url>http://www.rightplacetobuymeds.com/|buy cialis online</url> 8-DDD <url>http://www.medsmarkt.com/|viagra</url> :-))) <url>http://www.buycheapedmeds.com/|impotence solutions</url> 865
Saturday, 03 August 2013 10:24:51 (GMT Daylight Time, UTC+01:00)
<url>http://www.leadingautoinsurers.net/|auto insurance quotes</url> dykye <url>http://insuranceincolumbus.net/|car insurance quotes</url> 719129 <url>http://www.onlinecheapautoinsur.com/|car insurance quotes wisconsin</url> %-DD <url>http://www.bestautoinsurancepolicies.net/|auto owners insurance texas</url> imxd <url>http://insuredcar.net/|car insurance quotes</url> 855
Saturday, 03 August 2013 19:12:12 (GMT Daylight Time, UTC+01:00)
<url>http://www.getafirmeerection.com/|generac viagria</url> 8[ <url>http://www.getanonlinequote.net/|cheapest car insurance</url> %)) <url>http://www.topcarinsuranceproviders.net/|auto car insurance quotes</url> %-D <url>http://www.edmedsguide.net/|where to buy inexpensive viagra</url> 872 <url>http://www.buycheapedmeds.com/|erectile</url> fsalk
Tuesday, 06 August 2013 15:37:15 (GMT Daylight Time, UTC+01:00)
<url>http://carinsuranceinbaltimore.net/|car insurance quotes</url> ymfwky <url>http://chicagoinsuranceonline.com/|auto insurance websites IL</url> 78543 <url>http://www.buyautocoveronline.com/cheap-auto-insurance-IA.html|classic car cheap insurance Iowa</url> %-O <url>http://www.mycarinsuranceprovider.com/cheap-car-insurance-burlington.html|CHEAP CAR INSURANCE AND FREE QUOTE Burlington</url> :DD <url>http://lacarinsuranceonline.com/|Los Angeles buying temporary auto insurance for rental car</url> 19870
Thursday, 08 August 2013 04:40:18 (GMT Daylight Time, UTC+01:00)
<url>http://www.edtreatmentguide.net/|generic cialis</url> 8-O <url>http://cheapedpillsdrugstore.com/|buy brand viagra</url> =-PPP <url>http://www.strongererection.net/|CDs List Buy Cialis Online Pill</url> 99815 <url>http://www.helpforedonline.com/|cialis online</url> 374527 <url>http://www.onlineedrugs.com/|health impotence</url> %-DD
Saturday, 10 August 2013 18:00:19 (GMT Daylight Time, UTC+01:00)
<url>http://www.onlinecollegeoffers.net/|online degree programs accredited</url> 8-OO <url>http://getcheapinsurancehere.com/|cheap auto insurance</url> oeija <url>http://www.carinsurancechoice.net/|cheap car insurance quotes</url> 8-PP <url>http://www.autoinsurpolicy.com/|autobinsurance quotes</url> zxvj <url>http://wemeatagain.com/|viagra online</url> 8DDD <url>http://www.bestautoinsurancepolicies.net/|auto insurance</url> 67620 <url>http://www.mycarinsuranceprovider.com/|sell car insurance</url> abasb
Monday, 12 August 2013 14:07:50 (GMT Daylight Time, UTC+01:00)
<url>http://www.getafirmeerection.com/|buy viagra online</url> tgv <url>http://cheapedpillsdrugstore.com/|buy viagra</url> 698 <url>http://findyourdrugstore.net/|viagra for sale paying with mastercard</url> %-( <url>http://www.bestskincareproduct.net/|accutane buy on-line</url> hnckn <url>http://www.rightplacetobuymeds.com/|cialas for sale</url> 92812 <url>http://www.buycheapedmeds.com/|viagra cheapest prices</url> frduf
Wednesday, 14 August 2013 16:20:33 (GMT Daylight Time, UTC+01:00)
<url>http://www.carinsurancechoice.net/|car insurance quotes</url> %-))) <url>http://www.autoinsurpolicy.com/|auto insurance quotes</url> phwfr <url>http://www.onlinecheapautoinsur.com/|car insurance for cheap</url> vjywr <url>http://www.comparequotesonline.net/|get the best insurance quote</url> =)) <url>http://www.aboutcarinsurancerates.com/|shop low auto insurance california</url> 7511 <url>http://www.buyautocoveronline.com/cheap-auto-insurance-FL.html|cheap auto insurance</url> hgfkm
Thursday, 15 August 2013 18:45:51 (GMT Daylight Time, UTC+01:00)
<url>http://www.edtreatmentguide.net/|cialis cheap</url> vdrkr <url>http://www.topcarinsuranceproviders.net/|how to get the cheapest auto insurance in usa</url> unfgj <url>http://findyourdrugstore.net/|viagra</url> bcddo <url>http://www.helpforedonline.com/|online cialis cheap</url> 8807 <url>http://www.rightplacetobuymeds.com/|cialis tadafil</url> :[[[ <url>http://www.buycheapedmeds.com/|viagra online</url> 197066
Saturday, 17 August 2013 16:49:53 (GMT Daylight Time, UTC+01:00)
<url>http://www.carinsurquote.net/|vehicle insurance commercial</url> 93724 <url>http://findyourdrugstore.net/|order viagra online</url> 8-]]] <url>http://bestinternetdrugstores.com/|buy cialis</url> %]] <url>http://www.bestskincareproduct.net/|low dose accutane studies</url> evjgb
Sunday, 18 August 2013 15:40:50 (GMT Daylight Time, UTC+01:00)
<url>http://www.onlinecollegeoffers.net/|history degree programs</url> hbnz <url>http://www.buyautocoveronline.com/cheap-auto-insurance-SD.html|cheap auto insurance SD</url> kjspgc <url>http://canadianaudiophile.com/|Buy Cheap Generic Cialis</url> 224573 <url>http://www.aboutcarinsurancerates.com/|and homeowners insurance auto</url> rtx <url>http://www.topcarprotection.com/|car insuranse in florida</url> jrpzy <url>http://www.getacheapcoverage.com/|auto insurance online quotes ms</url> 840557
Tuesday, 20 August 2013 09:13:03 (GMT Daylight Time, UTC+01:00)
<url>http://www.getanonlinequote.net/|cheap nj car insurance for teens</url> 8) <url>http://cheapedpillsdrugstore.com/|buying viagra</url> gati <url>http://www.strongererection.net/|cheap generic cialis</url> vtsl <url>http://findyourdrugstore.net/|viagra</url> 264 <url>http://www.medsmarkt.com/|Viagra without Prescription</url> >:]]]
Tuesday, 20 August 2013 09:13:03 (GMT Daylight Time, UTC+01:00)
<url>http://www.carinsurquote.net/|male car insurance cheap</url> 7879 <url>http://cheappricesforedmedicine.com/|viagra online no prescription</url> caiey <url>http://www.strongererection.net/|online cialis</url> matl <url>http://www.bestskincareproduct.net/|accutane and irritable bowel disease</url> %( <url>http://www.edcureon.net/|Cialis buy</url> dkzml <url>http://www.safetreatment.net/|prednisone buy no prescription</url> %-)
Comments are closed.