views:

122

answers:

4

Hi ,

At the moment i'm useing .Net 3.0 but I don't get it how to use Automatic Properties .

For example if i want write this sample code via Authomatic Properties , What should i do ?

private string _name = string.Empty;
private string _family = string.Empty;
//A field with default value
private DateTime _releaseDate = System.DateTime.Now;


//ReadOnly Property
public string Name
{
    get {return _name; }
}

//enforce validation rules on setting 
public string Family
{
    get { _family; }
    set
    {
        if (value.Length < 3)
            return new Exception("Family need at least 3 character long");
        else
            _family = value;
    }
}

// A property from two other fields
public string FullName
{
    get { return _name + " " + _family; }
}

Thank you All for your responding , I got my answer

+6  A: 

You can't.

An Automatic Property simply creates a private backing field for you, and hides that from you. If you need to have logic in your property, you should implement it yourself.

With Automatic Properties you must have both a getter and a setter, but you can make the setter private eg:

public string Foo { get; private set; }

By the way, you can't return an exception from a string property. Exceptions should be thrown, not returned.

public string Family
{
    get { _family; }
    set
    {
        if (value.Length < 3)
            return new Exception("Family need at least 3 character long");
        else
            _family = value;
    }
}

This should probably read:

public string Family
{
    get { _family; }
    set
    {
        if (value.Length < 3)
            throw new Exception("Family need at least 3 character long");
        else
            _family = value;
    }
}
Winston Smith
Thank you .Yes , you are right Of course i should Throw , I just wrote without compile and testing .What's benefit of making private setter as you said ?
Mostafa
It means that only this class may set the value; thus values cannot be changed by other classes. Hence, you can use it to create immutable objects. See http://msdn.microsoft.com/en-us/library/bb383979.aspx
Winston Smith
+3  A: 

You say you're using .NET 3.0 - are you using VS2005? If so, you're using C# 2 which doesn't support automatically implemented properties.

If you're using VS2008 and targeting .NET 3.0, then they should work fine - but they wouldn't actually be applicable in this case anyway, as none of your properties are "trivial" properties. Automatically implemented properties are a compact way of writing:

private string foo;
public string Foo { get { return foo; } set { foo = value; } }

If your property doesn't follow that pattern (modulo access modifiers for the property) then automatic properties won't help you.

Jon Skeet
+2  A: 

Automatic properties are used when you simply want a property to 'repeat' a backing field.

i.e.

string _name;
public string Name { get { return _name; } set { _name = value; } }

can simply be rewritten as

public string Name { get; set; }

the compiler expands out a private randomly named backing field for you and writes code equivalent to the first code sample.

However, if you want to do anything special, like a default value, or validate a set value, then you can't use them.

The possible exception, actually, is default initialisation.

You can use a constructor to default initialise properties.

Andras Zoltan
+2  A: 

You can't. Automatic property can't be readonly and can't contain logic in body.

Automatic property is just a short form of this

private string _name;
public string Name
{
   get{return _name;}
   set{_name = value;}
}

To this

public string Name{get;set;}
ArsenMkrt