A class in C# is a blueprint or template for creating objects. It defines the properties (data) and methods (functions) that the objects created from the class will have. A class can be thought of as a “mold” from which individual objects are created. It specifies what data the objects will contain and what operations (methods) they can perform.
Characteristics of a Class
Properties (Fields): These are the data members of the class, typically variables that hold the state of an object.
Methods: Functions that define behaviors or actions that the class can perform.
Constructors: Special methods that are called when an object of the class is created, used to initialize an object’s state.
Access Modifiers: Keywords that specify the accessibility of the class and its members, such as public, private, protected, etc.
using System;
// Class definition
class Employee{
// Properties (Fields)
public string firstName;
public string lastName;
public int Age;
// Constructor to initialize the object
public Employee(string fname, string lname, int age) {
firstName = fname;
lastName = lname;
Age = age;
}
// Display Full Name Method
public void displayFullName() {
Console.WriteLine("Full Name: " + firstName+ ' '+ lastName);
}
}
Explanation of class:
Properties: The Employee class has three public fields, firstName, lastName and Age, which store the data for each object.
Constructor: The constructor Employee(string fname, string lname, int age) initializes the firstName, lastName and Age properties when a new Employee object is created.
Method: The displayFullName() is used to display full name.
What is Object
An object in C# is an instance of a class. When you create an object, you are essentially creating a specific entity based on the blueprint provided by the class. Each object has its own copy of the class’s properties and can invoke the class’s methods. An object is created using the new keyword, which calls the constructor of the class.
Characteristics of an Object
State (Data): Each object has its own set of data values stored in the properties (fields) defined by the class.
Behavior (Methods): Objects can invoke the methods of the class to perform actions or modify their state.
Identity: Each object is a distinct entity with its own identity, even if two objects have the same state.
Example of Creating Objects from the Employee Class:
class Program
{
static void Main()
{
// Creating objects (instances of the Employee class)
Employee emp1 = new Employee("John", "Taylor", 35);
Employee emp2 = new Employee("Tom", "Taylor", 30);
// Accessing properties
Console.WriteLine($"Employee 1: {emp1.firstName}, Age: {emp1.Age}");
Console.WriteLine($"Employee 2: {emp2.firstName}, Age: {emp2.Age}");
// Calling methods
emp1.displayFullName();
emp2.displayFullName();
}
}
Complete program of Employee:
using System;
// Class definition
class Employee{
// Fields (Attributes)
public string firstName;
public string lastName;
public int Age;
// Constructor to initialize the object
public Employee(string fname, string lname, int age) {
firstName = fname;
lastName = lname;
Age = age;
}
// Display Full Name Method
public void displayFullName() {
Console.WriteLine("Full Name: " + firstName+ ' '+ lastName);
}
}
class Program
{
static void Main()
{
// Creating objects (instances of the Employee class)
Employee emp1 = new Employee("John", "Taylor", 35);
Employee emp2 = new Employee("Tom", "Taylor", 30);
// Accessing properties
Console.WriteLine($"Employee 1: {emp1.firstName}, Age: {emp1.Age}");
Console.WriteLine($"Employee 2: {emp2.firstName}, Age: {emp2.Age}");
// Calling methods
emp1.displayFullName();
emp2.displayFullName();
}
}
Output:
Employee 2: Tom, Age: 30
Full Name: John Taylor
Full Name: Tom Taylor