Sam Leach
Sam Leach

Reputation: 72

Object Oriented - Class Variables

I am pretty new to OOP and looking into things in a bit more depth, but I have a bit of confusion between these 3 methods in C# and which one is best and what the differences are between 2 of them.

Example 1 So lets start with this one, which (so I understand) is the wrong way to do it:

public class MyClass
{
    public string myAttribute;
}

and in this way I can set the attribute directly using:

myObject.myAttribute = "something";

Example 2 The next way I have seen and that seems to be recomended is this:

public class MyClass
{
    public string myAttribute { get; set;}
}

With getters and setters, this where I dont understand the difference between the first 2 as the variable can still be set directly on the object?

Example 3 The third way, and the way that I understand the theory behind, is creating a set function

public class MyClass
{
    string myAttribute;
    public void setAttribute(string newSetting)
    {
        myAttribute = newSetting;
        //obviously you can apply some logic in here to remove unwanted characters or validate etc.
    }
}

So, what are the differences between the three? I assume example 1 is a big no-no so which is best out of 2 and 3, and why use one over the other?

Thanks

Upvotes: 1

Views: 150

Answers (4)

James
James

Reputation: 82136

One of the main principles of OOP is encapsulation, and this is essentially the difference between the first example and the other 2.

The first example you have a private field which is exposed directly from the object - this is bad because you are allowing mutation of internal data from outside the object and therefore have no control over it.

The other 2 examples are syntactically equivalent, the second being recommended simply because it's less code to write. However, more importantly they both restrict access & control mutation of the internal data so give you complete control over how the data should be managed - this is ecapsulation.

Upvotes: 0

Deadcow
Deadcow

Reputation: 121

Property is a syntactic sugar over private attribute with get and set methods and it's realy helpful and fast to type;

You may treat automatic property with { get; set;} as a public attribute. It has no additional logic but you may add it later without uset ever notice it. Just exchange

public string MyLine { get; set;}

to

string myLine;
public string MyLine 
{
    get { return myLine; }
    set { myLine = value + Environment.NewLine;  }
}

for example if you need so.

You can also easily create read only property as { get; private set }.

So use Properties instead of public attributes every time just because its easier and faster to write and it's provides better encapsulation because user should not be used get and set methods if you decide to use it in new version of yours programm.

Upvotes: 0

Razvan
Razvan

Reputation: 3152

What your asking here has to do with encapsulation in OOP languages.

The difference between them is in the way you can access the propriety of an object after you created an object from your class.

In the fist example you can access it directly new MyClass().MyAttribute whether you get or set it's value.

In the second example you declare 2 basic functions for accessing it:

public string MyAttribute 
{ 
   get {return myPrivateAttribute;}
   set {myPrivateAttribute = value;}
}

In the third example you declare your own method for setting the value. This is useful if you want to customize the setter. For example you don't want to set the value passed, but the value multiplied by 2 or something else...

I recommend some reading. You can find something here and here.

Upvotes: 0

SWeko
SWeko

Reputation: 30912

The second

public class MyClass
{
  public string MyAttribute { get; set;}
}

is basically shorthand for:

public class MyClass
{
  private string myPrivateAttribute;

  public string MyAttribute 
  { 
     get {return myPrivateAttribute;}
     set {myPrivateAttribute = value;}
  }
}

That is an auto-implemented property, which is exactly the same as any regular property, you just do not have to implement it, when the compiler can do that for you.

So, what is a property? It's nothing more than a couple of methods, coupled with a name. I could do:

public class MyClass
{
  private string myPrivateAttribute;

  public string GetMyAttribute()
  { 
     return myPrivateAttribute;
  }

  public void SetMyAttribute(string value)
  {
     myPrivateAttribute = value;
  }

}

but then instead of writing

myClass.MyAttribute = "something";
string variable = myClass.MyAttribute;

I would have to use the more verbose, but not necessarily clearer form:

myClass.SetMyAttribute("something");
string variable = myClass.GetMyAttribute();

Note that nothing constraints the contents of the get and set methods (accessors in C# terminology), they are methods, just like any other. You can add as much or as little logic as you need inside them. I.e. it is useful to make a prototype with auto-implemented properties, and later to add any necessary logic (e.g. log property access, or add lazy initalization) with an explicit implementation.

Upvotes: 3

Related Questions