csharp Get and Set

//standard get/set
private static int _myInt;
public static int myInt
{
	get { return _myInt; }
	set { _myInt = value; }
}

//get/set with check for null/empty string
private string myString = string.empty;
public string myString
{
	get 
	{ 
		if (string.IsNullOrEmpty(myString)) return "Not yet set.";
		else return myString;
	}
}

//get/set with range check
private static int _myInt;
public int myInt
{
	get 
	{ 
		return myInt;
	}
	set 
	{ 
		if (value > 100) myInt = 100;
		if (value < 1) myInt = 1;
	}
}

//get/set with method calls
private string _myString = string.empty;
public string myString
{
	get 
	{ 
		if (string.IsNullOrEmpty(myString)) return "Not yet set.";
		else return myString;
	}
	set
	{
		myString = value;
		SaveObject();
	}
}
Example for using get/set with Properties

Updated: Friday 18th March 2011, 06:45pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.