Getters and Setters in c#

Getters and Setters in c#

In the C# programming language, you can use getters and setters function methods to manage or control access to member fields of a user-defined class. Getters and setters provide C# programmers with the ability to read (get) or modify (set) the values ​​of private class fields in a controlled manner. These methods provide direct access to the fields of a C# class. You can use getters and setters class function methods (enabled properties) in any C# program to maintain or protect the data integrity of an existing class and to manipulate or control the proper order of data.

Getters and Setters in c#

So, let’s explore getters and setters class methods in C# programming.

Class properties with getters and setters.

In any C# program, a user-defined class property is a class member or field that allows the current program to read, write, or calculate the values ​​of the class’s private fields. C# users can define getters and setters to manage class properties. This allows the current program to control how a class property’s field element can be accessed or modified.

Syntax of getters and setters function method properties.

class Employee

{

// Here we define employee class private field

private string emp_name;

// Here we use Property with a getter and setter class method

public string Name

{

get { return emp_name; } // Getter method: here it returns the value of the current class field

set { emp_name = value; } // Setter method: Here it assigns a new value to the class field

}

}

Element of the getter and setter class methods.

  • Getter (get) – Here the getter class method helps C# users retrieve the value of an existing class field property.
  • Setter (set) – Here the setter class method helps C# users assign a value to a class field property.

Here’s an example of the getter and setter class methods.

  • Here the name field emp_name in the Employee class is defined as private, and the name class property represents it as a public property. This gives you direct, controlled access to that class field value. When C# users use this class property, the getter and setter methods are automatically invoked for you.

Using getters and setters in C#.

C# users can use the name field property to get or set the value of the name field in the existing Employee class.

Example of getters and setters.

Using System;

class employee

{

// here we define Private field in employee class

private string emp_name;

// here we define Public class property with getter and setter class method

public string Name

{

get

{

return emp_name; // here it returns Getter

}

set

{

emp_name = value; // here it set Setter

}

}

}

class program

{

static void Main()

{

Employee employees = new Employee();

// here it Using the setter class method to assign a value to the employee name field

employees.Name = “Harry Deora”;

// here we Using the getter class method to retrieve the value of the name field

Console.WriteLine(employees.Name);

}

}

Here getters and setters in example.

  • When employees.Name = “Harry Deora”; executes, it calls the setter method, which assigns the value “Harry Deora” to the private name field.
  • When the Console.WriteLine(employees.Name ) console statement executes, it calls the getter method, which retrieves the value of the name field.

Automatic Class Properties.

C# programming provides a shorthand syntax for getters and setters (class method properties) that do not require an explicit backing field. This is known as automatic getters and setters (class method properties). In this case, the C# language automatically generates a private backing field for the class property.

Example of Automatic Class Properties.

class Employee

{

// This creates an auto-implemented property with a getter and setter in the Employee class.

public string Name { get; set; } // This automatically creates a backing field for the Employee class.

public int Age { get; set; } // This automatically creates a backing field for the Employee class.

}

Here in the Automatic Class Properties example.

  • Here in the Employee class, the Name and Age class field properties do not have explicit backing fields defined. Instead, the C# compiler automatically generates them when needed. This makes your class program code shorter and more easily readable, especially when you don’t need any extra logic in the getter or setter methods.

Automatic Getters and Setters Class Properties example.

So, let’s use getter and setter properties in a C# program in the same way as before.

Using System;

class employee

{

// here we use Auto-implemented class getter and setter class properties

public string Emp_Name { get; set; }

public int Emp_Age { get; set; }

}

class program

{

static void Main()

{

// here we Create a Employee class object element

Employee employees = new Employee();

// here we Set employee class property values

employees.Emp_Name = “Bhavshi Deora”;

employees.Emp_Age = 24;

// here we Display employee class property field member values

Console.WriteLine(“Employee Name is – ” + employees.Emp_Name);

Console.WriteLine(“Employee Age is – ” + employees.Emp_Age);

}

}

Getters and setters with logic in C#.

C# users can add custom logic to getter and setter methods to apply certain rules or validation to a program. Getter and setter logic is often used to validate or modify the value of a class field being set or retrieved.

Example of adding logic to a getter and setter.

using System;

class Employee

{

private string emp_name;

// Here we use a Property with a custom getter and setter method

public string Name

{

get

{

return emp_name.ToUpper(); // Getter method – here it returns emp_name in uppercase format

}

set

{

// Setter method – here it validates the user input

if (!string.IsNullOrEmpty(value))

emp_name = value;

else

Console.WriteLine(“Please fill in the employee name.”);

}

}

}

class Program

{

static void Main()

{

Employee employees = new Employee();

// Here we are setting a valid employee name

employees.Name = “Siddhi Deora”;

// Here we are getting the employee name (that returns in uppercase format)

Console.WriteLine(employees.Name);

// Here we are trying to set an empty name

employees.Name = “”;

}

}

Getters and setters with logic in the example.

  • Here, the getter method in the Employee class returns the value of the employee name field in uppercase format.
  • Here, the setter class method ensures that the name in the Employee class cannot be set to an empty or null value.

Read-only and write-only class properties.

In the C# programming language, you can create read-only or write-only class properties by providing only a get or set accessor.

Example of a Read-Only Class Property.

A read-only property in a C# class is one where C# programmers provide a get accessor but no set accessor. This means the property cannot be changed from outside the current class.

class Employee

{

private string emp_name;

// Here we define a read-only class property

public string Name

{

get { return emp_name; }

}

public Employee(string emp_name)

{

this.emp_name = emp_name;

}

}

In this Read-Only Class case.

  • The Name property in the Employee class can only be read from outside the class, and its value can only be set in the constructor.

Example of a write-only class property.

A write-only property in a C# class is one where you provide a set accessor but no get accessor. This allows you to assign a value to a class in a program, but you cannot read it directly.

class Employee

{

private string emp_name;

// Here we define a write-only class property

public string Name

{

set { emp_name = value; }

}

}

In this case, the write-only class property.

  • Here, the Name property field in the Employee class can only be set, and the value cannot be obtained directly through the property.

A summary of the Getters and Setters methods in C# programming.

Getters methods in C#.

  • Getter methods are used to access the value of a class field or property in C# programming.
  • In C# programming, the get accessor method provides you with the ability to retrieve class data by controlling it.

Setter methods in C#.

  • In C# programming, it is used to set the value of a class field or property.
  • The set class accessor method can add validation logic or data manipulation before assigning a property value to a field in a class.

Automatic methods Properties in C#.

  • Automatic class methods in C# programming provide a shorthand method for creating properties with the default get and set class accessor methods.
  • In C# programs, the compiler automatically creates a private backing field for a class property field when needed.

Read-Only methods Properties in C#.

  • In C# programming, a programmer can also have a class field property with only a getter (no setter), so it cannot be modified externally to the class.

Write-Only Class Properties in C#.

  • In a C# program, you can have a class field property method with only a setter (no getter), so it can create data in the class, but not read it.

Custom Logic Class Properties in C#.

  • Getter and setter methods in a C# program allow you to apply validation to or modify class values ​​before accessing or assigning them.

Leave a Reply