difference between get/set and overload

hey I’m trying to upgrade my C# knowledge.

I’m learning to work with custom classes for inheritance and such…
i noticed two methods are most frequently used.

  1. setteres and getters like :
public class Item
{
	public string _itemName;
	public int _cost;
	public string _description;
		
	public Item()
	{
		_itemName = string.Empty;
		_cost = 0;
		_description = string.Empty;
	}
	
	public string ItemName
	{ 
		get { return _itemName; }
		set { _itemName = value; }
	}
	
	public int Cost
	{
		get { return _cost; }
		set { _cost = value; }
	}
	
	public string Description
	{
		get { return _description; }
		set { _description = value; }
	}
	
//some functions...
}
  1. constructors with overload like
public class Item
{
	public string _itemName;
	public int _cost;
	public string _description;
	//public bool _collectable;
		
	public Item(string itemName, int cost, string description)
	{
		this._itemName = itemName;
		this._cost = cost;
		this._description = description;
	}
// some functions...
}

assuming i didn’t get a total wrong idea about this… if so please do correct me.

thanks
J.

If you do nothing fancy when setting / getting your property you can simply do:

public int IntProperty { get; set; }

And skip the variable.

Concerning overloading the constructor, you can call the other constructor like this:

TestClass(int x){ ... }
TestClass(int x, int y) : this(x) { ... }

And the difference is…a property is something like a variable, though you can chose to do fancy things when
setting/getting in contrast to a variable.

testClass.IntProperty = 1;

http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx

A constructor is called when you instantiate a class like :

TestClass testClass = new TestClass(x,y)

Keep in mind though that a constructor can not be used when inheriting from MonoBehaviour.

so when to choose which is a matter of what you want to do with it in the functions or what i need the public field to be able to do? still a bit confusing, but i guess I learn by using them.
thx for the explanation.