Objects and Classes
Objects and Classes
Objects and Classes
Object
• object is an entity that has state and behavior.
– Here, state means data and behavior means
functionality.
– Object is an instance of a class. All the members of
the class can be accessed through object.
Eg:
Student s1 = new Student()
C# Class
• In C#, class is a group of similar objects. It is a
template from which objects are created. It can
have fields, methods, constructors etc.
Eg:
public class Student
{
int id;//field or data member
String name;//field or data member
}
C# Object and Class Example
using System;
public class Student
{
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void Main(string[] args)
{
Student s1 = new Student();//creating an object of Student
s1.id = 101;
s1.name = "Sonoo Jaiswal";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
C# Class Example 2: Having Main() in
another class
using System;
public class Student
{
public int id;
public String name;
}
class TestStudent{
public static void Main(string[] args)
{
Student s1 = new Student();
s1.id = 101;
s1.name = "Sonoo Jaiswal";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
C# Class Example 3: Initialize and
Display data through method
using System;
public class Student
{
public int id;
public String name;
public void insert(int i, String n)
{
id = i;
name = n;
}
public void display()
{
Console.WriteLine(id + " " + name);
}
}
C# Class Example 3: Initialize and
Display data through method
class TestStudent{
public static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s1.insert(101, "Ajeet");
s2.insert(102, "Tom");
s1.display();
s2.display();
}
}
C# Class Example 4: Store and Display
Employee Information
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public void insert(int i, String n,float s)
{
id = i;
name = n;
salary = s;
}
C# Class Example 4: Store and Display
Employee Information
public void display()
{
Console.WriteLine(id + " " + name+" "+sal
ary);
}
}
C# Class Example 4: Store and Display
Employee Information
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.insert(101, "Sonoo",890000f);
e2.insert(102, "Mahesh", 490000f);
e1.display();
e2.display();
}
}
C# Constructor
In C#, constructor is a special method which is
invoked automatically at the time of object
creation. It is used to initialize the data members
of new object generally. The constructor in C#
has the same name as class or struct.
There can be two types of constructors in C#.
– Default constructor
– Parameterized constructor
C# Default Constructor
using System;
public class Employee
{
public Employee()
{
Console.WriteLine("Default Constructor Invoked");
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
}
}
C# Parameterized Constructor
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int i, String n,float s)
{
id = i;
name = n;
salary = s;
}
C# Parameterized Constructor
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Sonoo", 890000f);
Employee e2 = new Employee(102, "Mahesh", 490000f);
e1.display();
e2.display();
}
}
C# Destructor
• A destructor works opposite to constructor, It
destructs the objects of classes. It can be
defined only once in a class. Like constructors,
it is invoked automatically.
C# Constructor and Destructor
Example
using System;
public class Employee
{
public Employee()
{
Console.WriteLine("Constructor Invoked");
}
~Employee()
{
Console.WriteLine("Destructor Invoked");
}
}
C# Constructor and Destructor
Example
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
}
}
C# this
In c# programming, this is a keyword that refers to
the current instance of the class. There can be 3
main usage of this keyword in C#.
• It can be used to refer current class instance
variable. It is used if field names (instance
variables) and parameter names are same, that is
why both can be distinguish easily.
• It can be used to pass current object as a
parameter to another method.
• It can be used to declare indexers.
C# this example
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name,float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
C# this example
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Sonoo", 890000f);
Employee e2 = new Employee(102, "Mahesh", 490000f);
e1.display();
e2.display();
}
}
C# Access Modifiers
Access Specifier Description
Public It specifies that access is not restricted.