Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - GETTERS & SETTERS


So, as we know a Human being can have a name, speaks a language and eats some kind of food based on their choice.


So, we have the below class Human.


Example :



class Human
{
	string name;
	string food;
	string language;

	public void eat()
	{
		System.Console.WriteLine("Eats "+food);
	}

	public void speak()
	{
		System.Console.WriteLine("Speaks "+language);
	}
}



Just imagine, when God had created the Human Beings, God had prepared a blue print stating, all Humans will have a common behavior of speaking and eating.


Also we need to create objects out of Human class.


Human human = new Human();

So far it's ok. A Human being is created. But how do we set the details for the Human. In simple words, how do we create the Human Being whose name is John and he speaks English and eats Burger.


For that we have something called getters and setters.


Note : To use getters and setters in C#, we need something called as Properties.

Getter and Setter using Properties


As the name suggests, the Setter method is used to set or assign values to a property(example:- name) of a class. And the Getter method is used to get the values of a property(example:- name) in a class.


Note : Don't get panicked by the below getters and setters. It might look different from other language. But we will make it simpler.

Let us modify the Human class using getter and setter methods :


Example :



class Human{

	string name;
	string food;
	string language;
	
	public string Name 
	{
		get { return name; }
		set { name = value; }
	}
	
	public string Food 
	{
		get { return food; }
		set { food = value; }
	}
	
	public string Language 
	{
		get { return language; }
		set { language = value; }
	}

	public void eat(){
		System.Console.WriteLine("Eats "+food);
	}

	public void speak(){
		System.Console.WriteLine("Speaks "+language);
	}
}



Getter and Setter for 'name'


public string Name
{
	get { return name; }
	set { name = value; }
}

So, to assign value to name, we have created a property called Name.


public string Name

And the getters and setters for name are inside the property Name,


public string Name
{
	get { return name; }
	set { name = value; }
}

Now, you might be thinking, well! What a mess.


Don't worry! Let us simplify in the next example.


How do we assign values to an object using Setter?


Example :



class Human
{
	string name;
	string food;
	string language;
	
	public string Name 
	{
		get { return name; }
		set { name = value; }
	}
	
	public string Food 
	{
		get { return food; }
		set { food = value; }
	}
	
	public string Language 
	{
		get { return language; }
		set { language = value; }
	}

	public void eat()
	{
		System.Console.WriteLine("Eats "+food);
	}

	public void speak()
	{
		System.Console.WriteLine("Speaks "+language);
	}
}

class CreateHuman
{
	public static void Main(string[] args)
	{
		Human human = new Human();
		human.Name = "John";
		human.Food = "Burger";
		human.Language = "English";
		
		human.eat();
		human.speak();
	}
}


Output :



  Eats Burger
  Speaks English

In the above code, we have created a Human object,


Human human = new Human();

Then we have set the value (i.e John) to the Property Name .


human.Name = "John";

And the Setter of Name gets called.


public string Name
{
	get { return name; }
	set { name = value; }
}

Setting the value John to the variable name.


string name;

Similarly, we have set other other values as well.


human.Food = "Burger";
human.Language = "English";

And the human object is populated with the values,

C_Sharp

Then the eat() method gets called,


human.eat();

Now, if we look at the eat() method,


public void eat()
{
	System.Console.WriteLine("Eats "+food);
}

It prints the food, that is present in human object.


Now, if we take a look at the human object,

C_Sharp

The food attribute contains Burger in it.


And if you see the output,


Eats Burger

Burger is printed.


Similarly, the speak() method is called.


human.speak();

And we get the below output,


Speaks English

Next, let us see, how do we use the getter in C#.


How do we get the values of a class property using Getter?


Example :



class Human
{
	string name;
	string food;
	string language;
	
	public string Name 
	{
		get { return name; }
		set { name = value; }
	}
	
	public string Food 
	{
		get { return food; }
		set { food = value; }
	}
	
	public string Language 
	{
		get { return language; }
		set { language = value; }
	}

	public void eat(){
		System.Console.WriteLine("Eats "+food);
	}

	public void speak(){
		System.Console.WriteLine("Speaks "+language);
	}
}

class CreateHuman
{
	public static void Main(string[] args)
	{
		Human human = new Human();
		human.Name = "John";
		human.Food = "Burger";
		human.Language = "English";
		
		System.Console.WriteLine("His name is "+human.Name);
		System.Console.WriteLine("He eats "+human.Food);
		System.Console.WriteLine("His language is "+human.Language);
	}
}


Output :



  His name is John
  He eats Burger
  His language is English

Similarly, to see the values of name, food and language properties, we have used getters.


So, we want to get the contents of name of the human class. And we have used the Name property once again(i.e. human.Name).


System.Console.WriteLine("His name is "+human.Name);

And the getter for Name gets called,


public string Name
{
	get { return name; }
	set { name = value; }
}

So, if you see the getter,


get { return name; }

It returns the value of name.


Similarly, the values of food and language gets called in the same way.


System.Console.WriteLine("He eats "+human.Food);
System.Console.WriteLine("His language is "+human.Language);