 https://www.slideshare.net/adeel02
 A class is a construct that enables you to create your
own custom types by grouping together variables of
other types, methods and events.
 It defines the data and behavior of a type.
 Supports the OOP principle "encapsulation“
 Fields and methods can be::
Instance (or object members)
Static (or class members)
Class definition consists of
 Class declaration
 Data Fields
 Constructors
 Properties
 Methods
class test{
public int a = 5, b = 10;
public void mul(){
Console.Write("The Result of
Multiply is = {0}", a * b);
Console.ReadLine();
}}
Classes
Classes are declared by using the keyword class
followed by the class name and a set of class
members surrounded by curly braces { }.
class House
{
Body
...
}
Basic syntax for class declaration
Class comprises a class header and class body.
class class_name
{
class body
}
Note: No semicolon (;) to terminate class block.
But statements must be terminated with a semicolon ;
 In C#, individual classes and class members need
to be prefixed with visibility modifiers.
 By default, if you declare a member variable (or
anything else) in a class but do not specify its
access level, the member is considered private
and cannot be accessed from outside, i.e. by a
non-member of that class.
 Therefore, to make a member accessible by other
classes, you must declare it as public.
 Object is an abstraction of real world entity.
 Syntax for declaring objects is as follows :
 MyClass objMyClass = new MyClass();
 It can have its own copy of data members.
 It can be passed to a function like normal variables.
 The members of the class can accessed by the object
using the object to member access operator or dot
operator(.).
 Create Object
 Call function and variables with the help of object
 MyClass objMyClass = new MyClass();
 objMycClass.function_name();
 objMycClass.variable;
Constructors
public MyClass(parameters) {...}
• new operator – create an instance of the class
MyClass objMyClass = new MyClass();
public Person()
{
name = null;
age = 0;
}
 class testt{
 public static int a = 5, b = 10;
 public static void mul(){
 Console.Write("The Result of Multiply is = {0}", a
* b);
 Console.ReadLine();
 }}
 MyClass objMyClass1 = new MyClass();
 objMycClass1.function_name();
 MyClass objMyClass2 = new MyClass();
 objMycClass2.function_name();
 MyClass.function_name();
Classes and Objects in C#

Classes and Objects in C#

  • 2.
  • 3.
     A classis a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.  It defines the data and behavior of a type.  Supports the OOP principle "encapsulation“  Fields and methods can be:: Instance (or object members) Static (or class members)
  • 4.
    Class definition consistsof  Class declaration  Data Fields  Constructors  Properties  Methods class test{ public int a = 5, b = 10; public void mul(){ Console.Write("The Result of Multiply is = {0}", a * b); Console.ReadLine(); }}
  • 5.
    Classes Classes are declaredby using the keyword class followed by the class name and a set of class members surrounded by curly braces { }. class House { Body ... }
  • 6.
    Basic syntax forclass declaration Class comprises a class header and class body. class class_name { class body } Note: No semicolon (;) to terminate class block. But statements must be terminated with a semicolon ;
  • 7.
     In C#,individual classes and class members need to be prefixed with visibility modifiers.  By default, if you declare a member variable (or anything else) in a class but do not specify its access level, the member is considered private and cannot be accessed from outside, i.e. by a non-member of that class.  Therefore, to make a member accessible by other classes, you must declare it as public.
  • 8.
     Object isan abstraction of real world entity.  Syntax for declaring objects is as follows :  MyClass objMyClass = new MyClass();
  • 9.
     It canhave its own copy of data members.  It can be passed to a function like normal variables.  The members of the class can accessed by the object using the object to member access operator or dot operator(.).
  • 10.
     Create Object Call function and variables with the help of object  MyClass objMyClass = new MyClass();  objMycClass.function_name();  objMycClass.variable;
  • 11.
    Constructors public MyClass(parameters) {...} •new operator – create an instance of the class MyClass objMyClass = new MyClass(); public Person() { name = null; age = 0; }
  • 12.
     class testt{ public static int a = 5, b = 10;  public static void mul(){  Console.Write("The Result of Multiply is = {0}", a * b);  Console.ReadLine();  }}
  • 13.
     MyClass objMyClass1= new MyClass();  objMycClass1.function_name();  MyClass objMyClass2 = new MyClass();  objMycClass2.function_name();  MyClass.function_name();