-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Class and Objects
- Create Multiple Objects of One Class
C# Class and Objects
- Class and Object are the basic concepts of Object-Oriented Programming which revolve around the real-life entities.
- An object in C# is the physical as well as logical entity whereas a class in C# is a logical entity only.
- A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
-
A class can contain the following:
fields - variables to store data
methods - functions to perform specific tasks - An object is an element of an application, representing an instance of a class.
- An entity that has state and behavior is known as an object e.g. chair, table, ball, cycle,, bike car etc. Example: Class: Animal; Object: Tiger, Elephant, Lion, Human, Cow, Dog etc.
- When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. However, the values of those attributes, i.e. the state are unique for each object.
- The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
- User can create multiple objects of one class.
C# Class and Objects – Sample Code
Syntax to declare a class:
class <class_name> {
field;
method;
}
//Defining a Student class.
Public class Student{
//defining fields
Public int rollno= 1;
Public string name = “Rahul";
//defining methods
public void schoolName()
{
Console.WriteLine("State Government School");
}
}
static void Main(){
Student student1=new Student(); //creating an object of Student
//Printing values of the object in Console
Console.WriteLine(student1.rollno); //accessing member through reference variable
Console.WriteLine(student1.name);
}
public class Student
{
//define fields
public int rollno;
public string name;
public int age;
//Define Method
public void schoolName()
{
Console.WriteLine("Kendriya Vidyala");
}
}
class Program
{
int i = 25;
public void add(int a, int b)
{
Console.WriteLine(a + b);
}
public int multiply(int x, int y)
{
int result = x* y;
return result;
}
static void Main()
{
Student s1 = new Student();
s1.rollno= 1;
s1.name = "Rahul";
s1.age = 10;
Console.WriteLine(s1.rollno); //print default values of data types
Console.WriteLine(s1.name);
Console.WriteLine(s1.age);
s1.schoolName();Student s2 = new Student();
s2.rollno = 2;
s2.age = 11;
s2.name = "Rajesh";
Console.WriteLine(s2.rollno);
Console.WriteLine(s2.name);
Console.WriteLine(s2.age);
s2.schoolName();Student s3 = new Student();
s3.rollno = 3;
s3.name = "Vinay";
s3.age = 9;
Console.WriteLine(s3.rollno);
Console.WriteLine(s3.name);
Console.WriteLine(s3.age);
s3.schoolName();Program obj = new Program();
Console.WriteLine(obj.i);
obj.add(10 ,20);
Console.WriteLine(obj.multiply(15, 10));
Console.ReadKey();
}
}