Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# class and object with examples


If you are a beginner and you learned C++ OOPS concepts, then it will be easy to understand a class and object in C#.


C# class -


Class is a user-defined data type which is a collection of data, and methods to manipulate and access that data. Classes are reference type and store into the heap.

In other words, A class is a blueprint for the data types. A class is a collection of methods, data, and logics.

C# Class members- 


As you know, a class can contain lots of data and methods. We create methods to perform the operation in C# programming. The data and function within a class are known as class members.


class square
{
    int a; // a is a data member of class square
    int b; // b is a data member of class square
    int c; // c is a data member of class square
}


C# Data members -


The class consists of data to perform the task in C#. Data members are those members that contain the data for the class. (field or constant).

C# Member function -

We create a function inside the class to create different functionality in C#. We can say functions as methods. Member functions are those members that provide functionality for manipulating the data in the class. They include the method, properties, constructors, and operator and finalize.


C# object -


An object is a blueprint of a class. An object is a real word entity in C#. The best example of an object is bicycles. The bicycle contains the properties current gear, current cadence, two wheels and behavior (change gears, brake) in common. We can say behavior as functionality in C# programming. Another object can be, car, pen, mobile, chair, laptop, etc. Let's have another example of a laptop. Laptop object is an instance of the class.


The properties(data) of the laptop are laptop color, laptop price, laptop processor and the behaviors (functionalities ) are playing videos, showing data, sending data, etc.

The object is a runtime entity, it is created at runtime. All the members of the class can be accessed through the object.
Let's create an example.

Record re=new Record(); //createing an object of Record(A class)  .


In the above example, Record is the type and re is the reference variable. The new keyword allocates memory at runtime.

 

How to create a class in C#? 


Unlike other programming, classes are defined with the class keyword. To create a class, we define a class with a class keyword and name.


We write methods and data inside the class block { }. Let's understand with class definition syntax.

 class class_name {
 Data Members;
 Methods;
}


In the above syntax, access specifier can be public, private, protected.

Class- Class is a keyword.

Class_name-A programmer can define class name according to need.


Let's create an example of class and object to find our area of the rectangle in C# programming.

C# class and object example 


using System;

namespace ClassApplication {
   public class rect{
	    public double length;   // Length of a Rectangle
      public double breadth;  // Breadth of a Rectangle
	  
     
       class Rectang{
      static void Main(string[] args) {
         rect rect1 = new rect();   // Declare rect1 of type rect
         rect rect2 = new rect();   // Declare rect2 of type rect
		  
       double area=0; 
         // rect1 specification

         rect1.length = 3.5;
         rect1.breadth = 3.0;

         // rect 2 specification
         
         rect2.length = 2.0;
        rect2.breadth = 6.0;
           
         // Area of a Rectangle 
		  area=rect1.length*rect1.breadth; 
		Console.WriteLine("Area of rect1 : {0}",  area);

         // Area of a Rectangle 2
         area=rect2.length*rect2.breadth; 
		  Console.WriteLine("Area of rect2 : {0}",  area);

         
      }
   }
	 
            }
         }

Compile and execute the above code. It gives the result as output. 

 Arae of rect1 : 10.5
Arae of rect2 : 12

In the above C# example, we created two classes and objects.

Let's create another example of class and object in C# programming. In this example, we are going to create a class of employee.

 C# example of employee class 


using System;  
   public class Employee
    {  
	   public string name;
       public int emp_id; 
	   public string emp_dept; 
	   public double emp_number; 
	   public double emp_salary; 
   
         
    public static void Main(string[] args)  
        {  
            Employee emp = new Employee();//creating an object of Employee
		     emp.name="Ram"; 
		      emp.emp_id=2072;
		emp.emp_dept = "IT";
		
		emp.emp_number=98812771267;
		emp.emp_salary=30000;
		
              Console.WriteLine("This is "+emp.name+".My id is "+emp.emp_id+". I am working in the "+emp.emp_dept+" department. My mobile number is "+emp.emp_number+" and salary is "+emp.emp_salary+".");  
        
  
        }  
    }  

 

Output- 

This is Ram. My id is 2072. I am working in the IT department. My mobile number is 98812771267 and salary is 30000.

In this way, we can create a class and object using C# programming.

 


Please Share

Recommended Posts:-