Results 1 to 8 of 8

Thread: [3.0/LINQ] Property Declartation

  1. #1

    Thread Starter
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Question [3.0/LINQ] Property Declartation

    Not really a question just wanting to see how others feel about the new property declaration

    Code:
          public string Name
            {
                get;
                set;
            }
    as opposed to

    Code:
         
    private string name;
    public string Name
            {
                get
               {
                   return name;
                }
                set
                {
                     name=value;
                  }
            }
    I personally prefer the older syntax as within a class the idea of calling the property never seems right to me I always call the private field just wanting to hear other coders opinions

  2. #2
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310

    Re: [3.0/LINQ] Property Declartation

    Our shop standard is unless you can get in the conference room in front of the the entire team and present an overwhelming case why you should use the private var we always call the property. You can probably guess we like the new syntax...

    I'd much rather type

    Code:
    public string Name {get; set;}
    fewer keystrokes, fewer opportunities to make a mistake.

    This assumes of course that you have no need to do any coding in the getter or setter.
    Last edited by VBGuy; Dec 4th, 2007 at 01:39 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Property Declartation

    The problem with accessing the private variable within the class is that it bypasses any logic you add to the property getter or setter. If it is specifically desirable to by pass that logic then you should access the variable but otherwise you should always access the property. One of the reasons for properties is to allow you to change the implementation without changing the interface. If you were to change the property implementation at some point then every place you access the variable within the class would bypass the new logic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [3.0/LINQ] Property Declartation

    That's awesome! I didn't know there was such a new feature in C# 2008! How cool is that? Way cool! So much less typing and confusion now and none of that, "should i access the variable or the propery stuff" and the, "should i put the private variable right next to the property or the other variables?"

    Also, none of that think up crazy names for the private variables so when your application is converted to vb .net it will still work stuff.

    Hah, this is way cool.

    *very pleased*



    Edit: Where do you find out about things like these? Is there a universal changelog avaiable somewhere on microsoft?
    Last edited by Fromethius; Dec 4th, 2007 at 07:50 PM. Reason: Thought of something else to say.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Property Declartation

    Quote Originally Posted by Fromethius
    Also, none of that think up crazy names for the private variables so when your application is converted to vb .net it will still work stuff.
    That was never a problem anyway. You just use "PropertyName" and "_propertyName" in both.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [3.0/LINQ] Property Declartation

    Quote Originally Posted by jmcilhinney
    That was never a problem anyway. You just use "PropertyName" and "_propertyName" in both.
    Hate that standard
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [3.0/LINQ] Property Declartation

    The underscores cut me at night =(

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Property Declartation

    The use of an underscore on variable names that provide a property value is unobtrusive and clear. That is something to strive for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width