×

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It can be anything that exists in the real world and that has state and behavior for eg. Car, pen, chair, etc. In C#, object is a real time entity. It is an instance of the class which can access all the fields and methods of the class. Let's see an example of creating object of a class using new keyword. Student s=new student(); // creating object of student class Example illustrates the creation of object of student class. s is the reference variable to the object for which the memory is provided at runtime by new keyword. C# Class: In C#, class is the group of similar type of objects. A class can have static and non static methods. It can have fields and constructors. It represents the template of its object.
Let's see an example of C# class.
public class student
{
int id; //integer field
string name; //string field
}
C# Object and class Example:
Let's see an example of class and object. The employee class has two fields named as id and name. the fields are initialized through object emp created in main method which is in same class.
using System;
public class Employee
{
            int id;
            string name;
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            Console.WriteLine(emp.name+" "+emp.id);
}
}
Output
Ram Sharma 100        
C# Class and object Example: Accessing class data from another class
We can also create object of the class in another class but the class fields and methods must be public.
using System;
public class Employee
{
            public int id;
            public string name;
            public void display()
            {
                        Console.WriteLine(id+" " +name);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            emp.display();
}
}
Output
100 Ram Sharma
C# Example: printing the class variables through method
using System;
public class Employee
{
            int id;
            string name;
            void display()
            {
                        Console.WriteLine(id+" " +name);
            }
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            emp.display();
}
}
Output
100 Ram Sharma        
C# classes and objects Example : Initializing class data by a method
Till now, we were initializing class fields in the main method. We can also create a method to initialize class data as shown below.
using System;
public class Employee
{
            public int id;
            public string name;
            public void setValues(int emp_id,string emp_name)
            {
                        id=emp_id;
                        name=emp_name;
            }
            public void display()
            {
                        Console.WriteLine(id+" " +name);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.setValues(200,"Harish Gupta");
            emp.display();
}
}
Output
200 Harish Gupta
C# Class and objects Examples: Storing and Retrieving information of various students
In the following Example, We are storing the student’s information through method and retrieving it.
using System;
public class Student 
{
            int roll_no;
            string name,course,batch;
public void setValues(int student_roll_no,string student_name,string student_course,string student_batch)
            {
                        roll_no=student_roll_no;
                        name=student_name;
                        course=student_course;
                        batch=student_batch;                      
            }
            public void display()
            {
                        Console.WriteLine(roll_no+" " +name+" "+course+" "+batch);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Student s1=new Student();
            s1.setValues(101,"Harish Gupta","B. Tech.","2016");
            Student s2=new Student();
            s2.setValues(102,"Ayush Sharma","B. Tech.","2017");
            s1.display();
            s2.display();
}
}
Output:
101 Harish Gupta B. Tech. 2016
102 Ayush Sharma B. Tech. 2017

Related Topics

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.