Results 1 to 2 of 2

Thread: c# .NET - basic - get & set

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default c# .NET - basic - get & set

    Hi,

    i'm programming for the first time in .NET (C#)

    I was wondering, what is the good solution to write getters en setters?

    Is this:
    Code:
    public void getVar(){
        return m_var;
    }
    public String setVar(String newVar){
         m_var = newVar;
    }
    or

    Code:
    public String var
        {
            get
            {
                return m_var;
            }
            set
            {
                m_var = value;
            }
        }
    or are both solutions correct?
    And if there is only one solution, can you explain why i can't use the other one?
    tnx
    Last edited by NDK; 11-27-2009 at 11:16 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    If you're coming from a language from Java, the former code is easier to work with.

    However, C# handles public properties more like the second block of code. So if you want to make your code feel like it was made for C#, I'd go with using the latter pattern.

  3. The Following User Says Thank You to techietim For This Useful Post:

    NDK (11-30-2009)

Bookmarks

Posting Permissions

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