C# notes part ->3
static
1. static data
2. static method
3. static constructor
Static data: The copy of the memory which is shared to shared members.
Class Student
{
regNo=100;
incr()
{
regNo++;
}
}
Student s1 = new Student();
s1.incr();
Student s2 = new Student();
s2.incr();
Student s3 = new Student();
s3.incr();
Static Variables and Functions:
Ø Each object has its own set of member variables.
Ø To retain the value of variable through out the program, we can declare the variable as a static variable.
Ø To manipulate and use the values of static variables, we can define a function as static function.
Ø Static function can access only static variables.
Ø Static functions can exist even before the object is created.
using System;
namespace staticvarfun
{
class Student
{
public static int regNo;
public static void Increment()
{
regNo++;
Console.WriteLine("RegNo:{0}",regNo);
}
}
class Program
{
static void Main(string[] args)
{
Student.regNo = 100;
Student.Increment();
Student.Increment();
Student.Increment();
Student.Increment();
Console.ReadLine();
}
}
}
Static Constructors:
Static constructor is a constructor which is invoked only once for the entire program whenever you create first instance or whenever you call a static function.
Write a program to generate RegNo for the Students from 1000.
using System;
namespace StaticConstructor
{
public class Student
{
static int num;
public int RegNo;
static Student()
{
num=1000;
}
public Student()
{
num++;
RegNo=num;
}
}
class Program
{
static void Main(string[] args)
{
Student s =new Student();
Console.WriteLine("RegNo# "+s.RegNo);
Student s1 = new Student();
Console.WriteLine("RegNo# "+s1.RegNo);
Console.ReadLine();
}
}
}
“this” keyword is used to hold the current objects.
using System;
namespace thiskeywordex
{
class Student
{
public int sid;
string sname, saddress;
public Student(int sid,string sname,string saddress)
{
this.sid = sid;
this.sname=sname;
this.saddress=saddress;
}
public void Display()
{
Console.WriteLine("The Student Id = {0}, Student Name = {1},Student Address ={2}",sid,sname,saddress);
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student(1000,"Rajitha","Hyderabad");
s.Display();
Console.ReadLine();
}
}
}
inheritance
Acquiring the fields and methods of a base class into the derived class is called as Inheritance.
Synyax:
Access specifier class classname
{
//Base fields
//Base methods
}
access-specifier class derivedclassname : baseclassname
{
//base members
//derived members
}
using System;
namespace Inheritance
{
public class Sample
{
protected int a = 5;
public void Display()
{
Console.WriteLine("the value of a = {0}",a);
}
}
public class Test : Sample
{
private int b=45;
public void Print()
{
Console.WriteLine("The value of b = {0}",b);
Console.WriteLine("The sum of the a and b is {0}",a+b);
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Display();
t.Print();
Console.ReadLine();
}
}
}
binding
It is a concept of providing interface pointers of a class to the object variable.
Types of Bindings:
1. Static binding
2. Dynamic binding
Static binding: the binding which is provided at the compile time.
The programmer job is easier and it is very faster than dynamic binding.
Eg:
Sample s = new Sample();
Dynamic binding: the binding which is provided at the runtime.
Only flexibility is the advantage with dynamic binding.
Eg: Sample s=new Sample();
s = new Test();
Dynamic binding designate the Polymorphism.
Polymorphism
It is a concept of providing single function with different behaviors or signatures.
There are two types of polymorphisms.
1. Static polymorphism
a. Operator overloading
b. Function overloading
2. Dynamic polymorphism
a. Virtual functions
b. Abstract class
Function overloading: it is a concept of providing same function name with different arguments.
using System;
namespace funoverload
{
public class overload
{
public void Add(int x, int y)
{
Console.WriteLine("the sum of x and y is {0}",x+y);
}
public double Add(int x, double y, int z)
{
return (x + y + z);
}
}
class Program
{
static void Main(string[] args)
{
overload a = new overload();
a.Add(10, 2);
double f=a.Add(2, 3.5, 7);
Console.WriteLine("the sum of x,y, and z is {0}",f);
Console.ReadLine();
}
}
}
Operator overloading: It is used to provide a additional meaning to the existing operator.
using System;
namespace operatoroverload
{
public class BinaryEx
{
public int a;
public BinaryEx()
{
}
public BinaryEx(int a)
{
this.a = a;
}
public static BinaryEx operator +(BinaryEx b1, BinaryEx b2)
{
BinaryEx b3 = new BinaryEx();
b3.a = b2.a + b1.a;
return b3;
}
public void Display()
{
Console.WriteLine("the value of a is {0}",a);
}
}
class Program
{
static void Main(string[] args)
{
BinaryEx x = new BinaryEx();
BinaryEx x1 = new BinaryEx(20);
BinaryEx x2 = new BinaryEx(40);
BinaryEx x3;
x3 = x1 + x2; //calls the operator overloaded function
x.Display();
x1.Display();
x2.Display();
x3.Display();
Console.ReadLine();
}
}
}
Virtual Functions: the function which contain a virtual modifier is called as virtual function.
Eg:
public virtual void Display
{
....
}
Overridden method: providing a additional meaning to the virtual function of a base class to the derived class is called as overridden method. This mechanism also called a s overriding. It is nothing but a function having the same name with same signature in both the base and derived classes.
This can occur only within the class.
using System;
namespace overrideappEx
{
public class Sample
{
public virtual void display()
{
Console.WriteLine("this is the Sample class display() method\n");
}
}
public class Test:Sample
{
override public void display()
{
base.display();
Console.WriteLine("this is the Test class display() method");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.display();
Console.ReadLine();
}
}
}
base keyword acts as a reference for the base class from the derived class.
Comments