Fields, Properties, and Methods in c#

Fields, Properties, and Methods in c#

Object-Oriented Programming (OOP) Concepts in the C# Programming Language Fields, properties, and class methods are the basic elements or portions of a user-defined class. Let’s understand each of these elements in C# programming, namely fields, properties, and methods, and how they are used in C# programming.

Fields, Properties, and Methods in c#

Fields, Properties, and Methods in the C# Object-Oriented Programming (OOP) Concepts.

Field in a C# Class.

A field in a class is a user-defined variable declared or defined within a class, structure, or interface. Fields declared in a class represent the state or data of a class object. The field element in a class is used to store and process data values ​​that are considered essential for the user-defined class object or class to function.

  • Class Field – A field is a variable in a user-defined class that contains class data that is mostly private in nature.
  • Class Access Modifier – Field elements used in a class are usually marked or declared as private so that they can be encapsulated within the existing class if needed and provide controlled access to them through class properties or methods.

Syntax of a field in a class.

class Employee

{

// Here we can declare important class Field declarations

private string emp_name; // Here we declare emp_name as a private class field

private int emp_age; // Here we declare emp_age as a private class field

}

In the above example.

  • In this example, emp_name and emp_age are fields of a user-defined class within the Employee class. These class-defined fields are private in nature, meaning they can only be accessed or modified within the current class.

Properties in a C# Class.

A property is a member in a user-defined class that provides a method to read, write, or process the value of a private field defined within the class. Class properties are often used as an alternative to class fields when you want to control access to a class field, add validation, or dynamically calculate class values.

  • Get Accessor – Retrieves the value of a property within a class.
  • Set Accessor – Sets the value of a property within a class.

Class properties in a C# program provide a clear, easy-to-maintain and control method for accessing user-defined class data. It also maintains the encapsulation features of the class.

Syntax of C# Class Properties.

class Employee

{

//  Here we define a private employee class field

private string emp_name;

//  Property with getter and setter

public string Name

{

get { return emp_; } // Getter method here returns the class field value

set { emp_name = data; } // Setter method here assigns a value to the field

}

//  Here it is a Property with an automatic getter and setter auto-implemented concept

public int Emp_Age { get; set; }

}

Class Properties Explanation.

  • In this example, the EmployeeName property manually defines both get and set accessor methods, allowing control over how the emp_name field in the Employee class is accessed or modified when needed.
  • The Emp_Age Employee class property is an auto-implemented class property. The C# compiler automatically creates a backing field for it, and C# doesn’t need to explicitly define it.

Using Class Properties.

class Employee

{

static void Main()

{

Employee employees = new Employee();

// Here we set property values ​​for the class object

employees.Emp_Name = “Harry Deora”; // Here we use the setter for the Emp_Name property

employees.Emp_Age = 24; // Here we use the setter for the employee Age property

// Here we get property values ​​for the class object

Console.WriteLine($”Employee Name is – {employees.Emp_Name}, Employee Age is – {employees.Emp_Age}”);

}

}

Class Methods in C#.

In a C# program, a method is a user-declared class function declared within a custom class or class structure that defines the behavior or current action of the current class object. Multiple class methods declared within a class can operate on the class’s fields and properties, accept variable parameter values ​​from the user-defined class, and return the desired value output if required.

  • Instance class methods – Instance class methods are connected to an instance of the class. To call these class methods, you must create a class object.
  • Static class methods – Static class methods are connected to the class, and you do not need to create an instance of the class to call a static class method.

Syntax of Class Methods.

class employee

{

// here we define an employee class Fields

private string emp_name;

private int emp_age;

// here we create a Constructor for employee class

public Employee(string emp_name, int emp_age)

{

this.emp_name = emp_name;

this.emp_age = emp_age;

}

//here we use Instance class method

public void EmployeeInfo()

{

Console.WriteLine($”Employee Name is – {emp_name}, Employee Age is – {emp_age}”);

}

// here we define a class Method with a return value

public int FindDob()

{

return DateTime.Now.Year – emp_age;

}

// here we define a Static method

public static string GetClassName()

{

return “Employee”;

}

}

Explaining Class Methods.

In this example, EmployeeInfo() is an instance class method that displays the details of an employee defined in the Employee class. FindDob() is an instance class method that finds, calculates, and displays the employee’s date of birth.

GetClassName() is a static function method associated with the Employee class that returns a class name.

Using the method in a C# program.

class Employee

{

static void Main()

{

//Here we are creating an object of the Employee class

Employee employees = new Employee(“Harry Deora”, 24);

// Here we are calling an instance of the class method

employees.EmpInfo(); // Result is – Emp_Name – Harry Deora, Emp_Age – 24

// Here it is calling a class method with a return value type

int dob ​​= employees.FindDob();

Console.WriteLine($”Date of Birth is – {dob}”); // Result is – Date of Birth is – 2007

// Here we use calling a static class method

Console.WriteLine($”Current Class Name is – {Employee.GetClassName()}”); // Result is – Current Class Name is – Employee

}

}

Important elements of a class: fields, properties, and methods.

Class fields in C#.

  • Fields declared in a class represent the class’s internal data.
  • They are typically marked or indicated as private to protect the class’s data encapsulation features.
  • Class fields are accessed directly or through properties and methods within the class.

Class properties in C#.

  • Properties defined within a class provide methods for getting and setting the values ​​of private fields within the class.
  • Class properties can have custom logic defined in the getter and setter.
  • When class properties do not require any additional class logic in the getter and setter, this simplifies the code for auto-implemented properties within the existing class.

Class methods in C#.

  • Fix the behavior of an object within a user-defined class.
  • They allow modifications to a class’s existing fields and their default properties.
  • User-defined classes can accept variables and parameters, and return class values ​​when necessary.
  • Instance methods defined within a class require an object of the class, or static class methods are connected to the existing class itself.

Access modifiers and encapsulation concepts in a class.

To encapsulate class data in a C# class program, class fields are typically declared private so that they cannot be directly accessed externally within the class. This allows us to control how private class members can be accessed and modified using existing class properties and methods.

Example of encapsulation with access modifiers.

class Employee

{

private string emp_name; // Here we declare emp_name as a private class field

private int emp_age; // Here we declare emp_age as a private class field

//  Here we use a public class property to control access to the ’emp_name’ class field

public string Name

{

get { return emp_name; }

set

{

if (!string.IsNullOrEmpty(info)) // here it check employee name Validation

emp_name = info);

otherwise

Console.WriteLine(“Employee Name Should be filled.”);

}

}

// here we use Public property to control access to ’emp_age’ employee class field

public int age

{

get { return emp_age; }

set

{

if (info) >= 0) // here it check employee age Validation

emp_age = info);

otherwise

Console.WriteLine(“Employee Age is .”);

}

}

// here we use displayinfo() Method to display information

public void DisplayInfo()

{

Console.WriteLine($”Employee Name is – {Name}, employee Age is – {Age}”);

}

}

Example of encapsulation with access modifiers.

  • Here, in the Employee class, the Employee fields emp_name and emp_age are defined as private class members, making them inaccessible to external users.
  • In the Employee class, the Name and Age fields are defined as class properties that provide controlled access to these Employee class fields, allowing validation before assigning any value to the Employee class.

Readonly fields in a class.

A readonly class field in a user-defined class can be assigned only once, either at the point of class declaration or as a parameter element within a user-defined class constructor. Readonly class fields are used when you want to create fields in a class that should never be modified after initialization in the current class.

class Employee

{

public readonly string DigitalId;

public Employee(string digitalId)

{

DigitalId = digitalId;

}

}

In the case of readonly fields.

  • In this program, the DigitalId class field can only be assigned when the existing class object is created, and cannot be modified after that.

Fields, Properties, and Methods Summary in C#.

  • Class fields – Store class data elements within a user-defined class. They are often declared as private members within the class for the sake of encapsulation.
  • Class properties – Provide controlled access to class fields with get and set data accessors in the existing class. This allows defining custom logic for getting/setting values ​​in the existing class.
  • Class methods – Define the default behavior of a class. It can take input variable parameters from the user into the class, and return values ​​to the class if needed. Class users can manipulate the fields and properties of a class with it.

Leave a Reply