×

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# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 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# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 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# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute 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# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 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# 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# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

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...

6 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.

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.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.