C# Dot Net tutorials
WELCOME TO C# DOT NET PROGRAMMING
Monday, November 9, 2009
UNBOXING SAMPLE
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
int i = 5;
Object obj = i; // implicit boxing
int j = (int)obj;// explicit un-boxing
}
}
}
BOXING SAMPLE PROGRAM
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
int i = 10;
Object obj = i; // implicit boxing
obj.ToString();
}
}
}
BOXING AND UNBOXING
Unboxing is explicit conversion of object type to value type.
THE SEALED KEYWORD
public sealed class demo
{
public int a = 10;
public int b = 2;
public void add()
{
int c = a + b;
Console.WriteLine(c);
}
}
Now I cant inherit this class 'SINCE IT CONTAINS A KEY WORD SEALED'.
INHERITANCE IN C#.NET
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication14
{
class MyClass
{
public int x, y;
public void readvalue()
{
x = 10;
y = 20;
}
public void printvalue()
{
Console.WriteLine("The Value of X= " + x);
Console.WriteLine("The value of Y= " + y);
}
}
class MainClass
{
public static void Main()
{
MyClass1 m = new MyClass1();
m.readvalue();
m.readnextvalue();
m.printvalue();
m.printnextvalue();
Console.ReadLine();
}
}
class MyClass1 : MyClass
{
public int p, q;
public void readnextvalue()
{
p = 30;
q = 40;
}
public void printnextvalue()
{
Console.WriteLine("The Value of P= " + p);
Console.WriteLine("The Value of Q= " + q);
}
}
}
This is the sample program to demonstrate inheritance.
The operator used for inheritance is ‘:’
the syntax is Class derived class: baseclass
In the above program,
class MyClass1 : MyClass
Shows that we are inheriting Myclass1 Form Myclass.
RUNTIME POLYMORPHYSM
Here the method name is same and the number as well as the type of the argument is the same. So, while executing which method is called first is to be determined at run time. For that we have to use two key words like virtual and override.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication14
{
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing Shape...");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing Circle...");
}
static void Main()
{
Shape theShape = new Circle();
theShape.Draw();
}
}
}
METHOD OVERLOADING POLY MORPHISM (COMPILE TIME)
What is method over loading?
We can have more than one method in a class with a same name but different in number or type or the argument (parameters), this is called method overloading.
Let us see a sample program to explain this concept:
using System;
using System.Collections.Generic;
using System.Text;
namespace forblog
{
class demo
{
void add(int a, int b)
{
int c=a+b;
Console.WriteLine("The Sum is "+c);
}
void add(float x, float y)
{
float z=x+y;
Console.WriteLine("The Sum is "+z);
}
public static void
{
demo ob=new demo();
ob.add(10,20);
ob.add(23.4f,34.4f);
}
}
}
This is the sample program which contain same method name add but when you see the type of argument inside it, is different. We can also do it by making difference in the number of arguments. please try it your self.
And now …..Why it is called compile time polymorphism.
Do you know what is happening during compilation in program? During compilation the syntax of our program is checked. So, here in the above concept we are making difference in the syntax i.e. in first add method we have given two integers and in second add method, we have two float.
so this is an example for method overloading.