Beruflich Dokumente
Kultur Dokumente
Classes
Objects
Methods
constructors
properties
access modifiers
method overloading
Inheritance
Polymorphism
Boxing and
UnBoxing
Classes
A class is simply an abstract model used to
define a new data types.
Basis for the object-oriented programming
Group of related methods and variables
A class is a template for an object
class MyClass
{
// fields, operations and properties go here
}
Syntax of class
class classname
{
type instance-variable1;
type instance-variable2;
//
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodnameN(parameter-list)
{
// body of method
}
}
class ProgramHELLO
{
static void Main(string[] args)
{
Console.WriteLine("WELCOME TO C#");
Console.ReadLine();
}
}
Objects
building blocks of object-oriented programming.
Each object is a programming unit consisting of data
(instance variables) and functionality (instance
methods).
A static method can not access the non static data
members or methods without object .
An object is created in the memory using the
keyword 'new'
Syntax:
classname obj_name=new classname();
using System;
class obj
{
public void show()
{
System.Console.WriteLine (hello) ;
}
public static void Main (String []args)
{
obj ob=new obj();
ob.show();
}
}
Method
Methods are the operations performed on the
data. A method may take some input values
through its parameters and may return a value
of a particular data type
A method is a member that implements an
action that can be performed by an object or
class.
A method is a function owned by your class.
Syntax :
type method_name (parameter-list)
{
// body of the method
}
using System
class meth
{
public void show()
{
System.Console.WriteLine (hello) ;
}
public static void Main ()
{
meth ob=new meth();
ob.show();
}
}
Constructors
Constructors are a special kind of method. A Constructor has
the following properties
It has the same name as its containing class
It has no return type
can have parameters.
It is automatically called when a new instance or objectof a
class is created,hence why it's called aconstructor.
class Super
{
public int length;
public int breadth;
public Super(int x,int y)
{
length = x;
breadth = y;
}
public int Display()
{
return(length * breadth);
}
}
class test
{
public static void Main()
{
Super s = new Super(10, 20);
int i = s.Display();
Console.WriteLine("Multiplication is :" +i);
Console.ReadLine();
}
Destructor
Destructs an instance of a class
A destructor method is called just prior to an object's final
destruction by the garbage collector.
A destructor can be used to ensure that an object terminates
cleanly.
A destructor is a function with the same name as the name of
the class but starting with the character ~.
class A
{
public A()
{
Console.WriteLine("creation A");
}
~A()
{
Console.WriteLine("destroying A");
}
}
class B : A
{
public B()
{
Console.WriteLine("creation B");
}
~B()
{
Console.WriteLine("destroying B");
}
}
class C : B
{
public C()
{
Console.WriteLine("creation C");
}
~C()
{
Console.WriteLine("destroying C");
}
}
class MainClass
{
public static void Main()
{
C c = new C();
Console.WriteLine("object created");
Console.WriteLine("press enter to destroy");
c = null;
Console.ReadLine();
}
}
Properties
It is the new feature implimented in c#.
Usually used for geting and setting the values for the data
members
Properties are named members of classes, structs, and
interfaces
They provide a flexible mechanism to read, write, or compute
the values of private fields through accessors.
Syntax
acc- modifier ret-type propertyName
{
get // Return the value to caller
{
return(var);
}
Set // Assign the value to particular variable
{
var=value
}
}
Example program
class Myclass
{
int x;
float y;
public int xprop
{
get
{
return (x);
}
set
{
x = value;
}
}
class MainClass
{
public static void Main()
{
Myclass m = new Myclass();
m.xprop = 10;
m.yprop = 10.3f;
Console.WriteLine("x=: " + m.xprop);
Console.WriteLine("y=: " + m.yprop);
Console.ReadLine();
}
}
Access modifiers
Access Modifier Description
Private
protected internal
Internal
protected
public
Method overloading
It is possible to have more than one method
with the same name and return type but with a
different number and type of arguments
(parameters). This is called method
overloading
class demo
{
void add(int a, int b)
{
int c=a+b;
System.out.print(c + , );
}
void add(float x, float y)
{
float z=x+y;
System.out.println(z);
}
public static void main(String arg[])
{
demo ob=new demo();
ob.add(10,20);
ob.add(23.4f,34.4f);
}}
Polymorphism
Overriding the methods - virtual and override
keywords
The new keyword
Boxing and Un-boxing
Inheritance
Inheritance is the process by which one object
acquires the properties of another object.
A class that is inherited is called a superclass. The
class which does the inheriting is called a subclass
Base Class
Derived Class
Derived Class
Derived Class
Types of Inheritance
Single Inheritance.
Multilevel Inheritance.
Multiple Inheritance.
Syntax of Inheritance
Derive class: Base class
Inheritance in C#
C# allows only single class inheritance.
Multiple inheritance of classes is not allowed
in C#.
Interfaces in C# can inherit more than one
interface
multiple inheritance of interfaces is allowed in
C#
Implementing inheritance in C#
class MyClass
{
public int x, y;
public void readxy()
{
x = 10;
y= 20;
}
public void printxy()
{
Console.WriteLine("X= " + x);
Console.WriteLine("Y= " + y);
}
}
class MainClass
{
public static void Main()
{
MyClass1 m = new MyClass1();
m.readpq();
m.readxy();
m.printpq();
m.printxy();
Console.ReadLine();
}
}
using System;
public sealed class demo
{
public int a = 10;
public int b = 2;
public void add()
{
int c = a + b;
Console.WriteLine(c);
}
}
public class demo1 : demo
{
public void sub()
{
int c = a - b;
Console.WriteLine(c);
}
}
class sample
{
public static void Main()
{
demo1 d = new demo1();
d.sub();
d.add();
Console.ReadLine();
}
}
Polymorphism
Polymorphism is the ability for classes to
provide different implementations of methods
that are called by the same name.
Polymorphism allows a method of a class to be
called without regard to what specific
implementation it provides.
Unboxing
int i = 5;
Object obj = i; // implicit boxing
int j = (int) obj;// explicit un-boxing