0

I've tried to learn the short version of get & set in C#, but I don't know how to use them.

This is what I tried:

namespace SomeNamespace {
    class SomeClass {
        private int field1 { get; set;}
        private int field2 { public get; public set; }
    }

    class OtherClass {
        SomeClass sc = new SomeClass();
        int field1 = sc.field1;           //it doesn't work
        int field2 = sc.field2;           //it also doesn't work
        sc.field1 = 1;                    //same here
        sc.field2 = 2;                    //and here
    }
}

In my SomeClass object I don't have access to any field nor "special" method to do this.

I obviously don't get it, so please help me to understand.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
user3616181
  • 975
  • 2
  • 8
  • 27
  • The getter and setter can be *as strict* or *more strict* than the property accessors. – Jens Kloster Oct 19 '14 at 09:09
  • 1
    Clarify "doesn't work"... – Ondrej Tucny Oct 19 '14 at 09:11
  • 2
    `field1` and `field2` are not *fields*, they are *properties*. *Fields* are basically variables which belong to an instance of a class. *Properties* on the other hand are like methods, but with a nice syntax for getting and setting values. – Dirk Oct 19 '14 at 09:12
  • @Dirk: What is the real life difference between field and property? Is there are reasons to use any of them? Or they differ only the way of getter and setter declaration? – user3616181 Oct 19 '14 at 09:18
  • @user3616181 You can find an explanation [here](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c). In general you should avoid public fields, because they make your implementation details visible to the outside. – Dirk Oct 19 '14 at 09:22
  • @Dirk I know that I sould avoid them, but I still just don't get the difference between public field and this construction (property I guess). They both have public modificator (I hope that's what you call it in english) so I'm a little bit confused. I hope your explanation link will help me to understand the difference. If not - huh, I will use the "normal" verion of getter and setter ;) – user3616181 Oct 19 '14 at 09:30
  • Oh, I think I slowly starting to understand :) Using property I useing more a method than accessing a field directly, but it use a syntax like normal assignment and getting value (I don't know any "wise" word for that :/) so it's really convenient. The public modificator confused me*, but now I think I understand. Am I right? *I thought that with {private set} I will still can do an assigment because it is public field. – user3616181 Oct 19 '14 at 09:44

3 Answers3

3

You need to use the accessors the other way around on your properties if you want to only allow read access on your property from outside classes:

public int field2 { get; private set; }
// setting only allowed from SomeClass, not from OtherClass or inheritors

To allow inheritors, you need to set private to protected.

If you want to allow both read and write from outside classes:

public int field2 { get; set; }
// setting allowed from any class
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

You need to declare them as public. Like following.

namespace SomeNamespace {
    class SomeClass {
        public int field1 { get; set;}
        public int field2 { get; set;}
    }

    class OtherClass {
        SomeClass sc = new SomeClass();
        // frist set the values
        sc.field1 = 1;                    
        sc.field2 = 2;                    
        // then read them 
        int field1 = sc.field1;           
        int field2 = sc.field2;           

    }
}

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

There are advantage of having getter/setter ( in comparison to just public variables).

  1. Set accessibility via private set; etc..
  2. You can add validation while setting the value or format while getting the value.
  3. You can use them as part of an interface definition or an abstract class.

SOUREC - http://msdn.microsoft.com/en-us/library/bb384054.aspx

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • Yeah, but that's mean I can access them as normal public field, so technically I don't need getter and setter... I know the convention (PascalCase for public things), but in fact that's like just accessing the public field. I'm a little bit confused... :D – user3616181 Oct 19 '14 at 09:09
  • Yes you are right, but with this type of getter/setter method you can set restriction to `write only` or `readonly property` , like just set `private set` to make readonly and vice versa. – Arindam Nayak Oct 19 '14 at 09:12
0
public class SomeClass
{
    //Will be accessible by instance of this class 
    public int Field1 { get; set; }

    //Accessible within class methods only
    private int Field2 { get; set; }

    public void SomeMethod()
    {
        //You can use private property in any of method within class only
        Console.WriteLine(Field2);
    }

    //Accessible from derived class
    protected int Field3 { get; set; }

}

public class SomeDerived : SomeClass
{
    public void SomeDerivedFunction()
    {
        //Accessing baseclass Property
        Console.WriteLine(Field3);
    }
}

public class SomeThirdPartyClass
{
    private SomeClass sc;

    public SomeThirdPartyClass()
    {
        sc = new SomeClass();

        //Field one as public accessible in other classes by instance
        Console.WriteLine(sc.Field1);
    }
}
Rajnikant
  • 2,176
  • 24
  • 23