views:

18

answers:

1

I think we can all agree that Automatic Properties in C# 3.0 are great. Something like this:

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

Gets reduced to this:

public string Name { get; set; }

Lovely!

But what am I supposed to do if I want to, say, convert the Name string using the ToUpperInvariant() method while "setting". Do I need to revert back to the old C# 2.0 style of creating properties?

    private string name;
    public string Name
    {
        get { return name; }
        set { name = value.ToUpperInvariant(); }
    }

Or is there a more elegant way of accomplishing this?

+2  A: 

Yes, you have to convert it back. An autoproperty can't do this kind of checks.

tanascius
Thanks for the confirmation! Every reference I had ever read about Auto-Properties had never mentioned anything about this. It seemed implied by how some articles and books were written, but nothing was ever explicitly stated that it was, in fact, this way. Thanks again for confirming my suspicions.
Pretzel
@Tanascius: Looks like my awarding you the "Green check" pushed you up to 10K. Congrats! You can moderate now. :D Please be kind to the rest of us underlings... :O
Pretzel
@Pretzel: Thanks, you are right, it was your "green check" ^^ that was a long way to go to get this 10K.
tanascius