user3616181
user3616181

Reputation: 1055

How to use short get set for private fields?

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.

Upvotes: 0

Views: 1884

Answers (3)

Arindam Nayak
Arindam Nayak

Reputation: 7462

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

Upvotes: 1

Rajnikant
Rajnikant

Reputation: 2236

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);
    }
}

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 156948

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

Upvotes: 3

Related Questions