Class and Objects in C# with
Examples
Hi Guys, Welcome in my online
tutorials.
In this online tutorial, I am going to explain
you the basic understanding of Class and Objects in C# with multiple examples.
Before proceeding to this article, please read our previous
article where we discussed the basics concepts of Object-Oriented
Programming.
For every developer, Understanding of class and objects in C# is
very important. Because everything in C# is in the form of class and objects.
In this tutorial, we are going to cover the
following basic questions in detail to understand the Class and Objects.
- Class and Objects in general term?
- What is a Class in C#?
- What are the different Types of classes
in C#?
- How to create a class in C#?
- How to create objects in C#?
- Difference between Class and Objects in
C#.
Class
and Objects in general term.
In simple term, Class is a template or blueprint with state and
behavior. State is called properties of a class and behavior is called actions
in a class.
Ex: Employee has the
properties like Id, Name, Address etc. and actions like AccountDetails, SalaryDetails
etc.
In simple term, Object is an instance of a
class.
Ex: If we consider the Employee in a company. Director, CTO, CEO,
Manager, Team Leader, Sr. Developer, Developer, etc. all are object of that
class Employee.
What
is a Class in C#?
We know that Class is a template or blueprint with state and
behavior. State is called properties of a class and behavior is called actions
in a class.
Remember: Action is called
method or function in C#.
We know that C# is an object-oriented programming language, so a
program is designed with objects and classes. A class is a code block that is
used for grouping properties/variables and methods/functions for developing functionality
logic.
What
are the different Types of classes in C#?
In general we use below 5 types of classes in C#:
1.
Static class
2. Sealed class
3. Partial class
4. Concrete class
5. Abstract class
Using access modifier we are restricting the
accessibility of the classes to the other classes.
Access modifiers are mainly 5 types:
- Public
- Private
- Protected
- Internal
- Protected internal
Key
features of a class:
- Class is a reference type. It means it
hold the object reference that is created dynamically in a heap.
- The base type of a class is System.Object.
- Default access modifier of a class
is Internal.
- Default access modifier of properties/
variables and methods is Private.
- Private classes are not allowed
directly inside the namespaces.
How to create a Class in C#?
The general form to create a class is as below:
<access modifier> class ClassName
{
//Properties/variables of this class
<access modifier> <return type> property1
<access modifier> <return type> property2
….
<access modifier> <return type> propertyn
//methods of this class
<access modifier> <return type> method1(parameters)
{
//method1 body
}
<access modifier> <return type> method2(parameters)
{
//method2 body
}
….
<access modifier> <return type> methodn(parameters)
{
//methodn body
}
}
Remember: access modifier
describe the accessibility, Data type describe the type of variable, and return
type describe the data type of the data, the method returns if any.
Ex:
using
System;
namespace
EHDApplication {
Public
class Employee {
private string id { get; set; } // Id of the Employee
public string name { get; set; } // Name
of the Employee
private int age { get; set; }; // Age of the Employee
public string Department { get; set; }// Department
of the Employee
public dynamic UserDetails(string Id) {
// Employee User Detail logic
}
public dynamic MonthlySalary(string Id) {
// Employee Monthly Salary logic
}
}
How to create an Object in C#?
We already discussed that an Object is a real-world entity. It is
a collection of leaving and non-leaving things. For example, Person, Animal, mobile,
laptop, pen, etc. In general object is an entity that has state and behavior.
Here, state means data and behaviors means functionality.
Object is created at runtime, that’s why it is called a runtime
entity.
It is an instance of a class. We can access all the members of a
class through the object. Use the dot (.) notation to access the members of a
class.
Ex: We can create an object using the new keyword.
Employee myEmployee = new Employee(); //creating an object of Employee
In this example, Employee is the type and myEmployee is the
reference variable that refers to the instance of Employee class. The new
keyword allocates memory at runtime in the Heap.
Example: Employee class that has three properties:
id, name, and department. It creates an instance of the class Employee,
initializes the object and prints the object value.
using System;
namespace EHDApplication {
public class
Employee
{
//
Declare properties/variabls
Id
{ get; set; } // Id of the Employee
int id { get; set; } // Id of the Employee, by default id is Private member
string
name { get; set; } // by default name is
Private member
string department { get; set; } // by default department is Private member
public static void Main(string[] args)
{
//creating an object of
Employee class
Employee
myEmployee = new Employee();
//Object initialization
myEmployee.id
= 1001;
myEmployee.name
= "Donald Trump";
myEmployee.department
= "IT";
//Printing the values
Console.WriteLine("Employee
ID : " + myEmployee.id);
Console.WriteLine("Employee
Name : " + myEmployee.name);
Console.WriteLine("Employee
Department : " + myEmployee.department);
}
}
}
OUTPUT:
Employee ID: 1001
Employee Name: Donald Trump
Employee Department: IT
Example: If Main () method in
another class then how can access class members.
Remember: In this case class must be public and we
need to specify the class member as public.
using System;
namespace EHDApplication {
public class
Employee
{
public int id{ get; set; }
public String name{ get; set; }
public string
department{ get; set; }
}
public class
EmployeeDemo
{
public static void Main(string[] args)
{
// creating an object of
Employee class
Employee
myEmployee = new Employee();
//Object initialization
myEmployee.id
= 1001;
myEmployee.name
= "Donald Trump";
myEmployee.department
= "IT";
//Printing the values
Console.WriteLine("Employee
ID : " + myEmployee.id);
Console.WriteLine("Employee
Name : " + myEmployee.name);
Console.WriteLine("Employee
Department : " + myEmployee.department);
}
}
}
OUTPUT:
Employee ID: 1001
Employee Name: Donald Trump
Employee Department: IT
Example: Initialize and Display
data through a method
In this example we are initializing and
displaying an object through the method.
using System;
namespace EHDApplication {
public class
Employee
{
public int id{ get; set; }
public String name{ get; set; }
public string
department{ get; set; }
public void
InsertEmployData(int ID, string Name, string Department)
{
id
= ID;
name
= Name;
department
= Department;
}
public void
DisplayEmployeeData()
{
Console.WriteLine("Employee
ID : " + id);
Console.WriteLine("Employee
Name : " + name);
Console.WriteLine("Employee
Department : " + department);
}
}
public class
EmployeeDemo
{
public static void Main(string[] args)
{
//creating two objects of
Employee class
Employee
myEmployee1 = new Employee();
Employee
= new Employee();
//Initializing the Employee
Object myEmployee1 using
method
myEmployee1. InsertEmployData (1001, "Donald Trump ", "IT");
//Initializing the Employee
Object myEmployee2 using
method
myEmployee2. InsertEmployData (1002, "John Smith", "HR");
myEmployee1. DisplayEmployeeData ();
myEmployee2. DisplayEmployeeData ();
}
}
}
OUTPUT:
Employee ID: 1001
Employee Name: Donald Trump
Employee Department: IT
Employee ID: 1002
Employee Name: John Smith
Employee Department: HR
What are the difference between Class and Objects in C#
We learned that Class is a template for Objects.
Each and every Object must belong to a specific Class.
Remember: All Objects share the same copy of the methods, but
maintain a separate copy of the member data (Properties or fields).
Ex: In previous example Employee class have separate copy of the
member data (Properties or fields).
Employee ID: 1001
Employee Name: Donald Trump
Employee Department: IT
Employee ID: 1002
Employee Name: John Smith
Employee Department: HR
But share the same copy of the
methods:
InsertEmployData() & DisplayEmployeeData ()
The main difference between
class and object is as below:
Class
|
Object
|
Class is a template/blueprint for Objects
|
Instance of a class
|
It is used to bind data and methods in a single unit.
|
It is used as a variable of a class
|
It does not take any memory spaces after executing the
code.
|
It take memory spaces in heap after executing the code.
|
In this article, I try to explain Class and Objects in C# with some examples. I
hope you understood class and objects in C#. In the next article, I am going to discuss Constructors
in C# and its types in details with examples.
0 comments:
Post a Comment