Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers
Boxing - The term boxing stands for converting value types to Object type. Boxing is the ultimate way to treat value type as Object type.The type conversions are happened internally from value type to Object type.
In below example the int is the value datatype converted into Object type using Object Data type.Unboxing - The term Unboxing stands for converting Object types to value type vice versa of Boxing. It is the opposite of the Boxing. These conversions are also happened implicitly.
ExampleVIRTUAL and OVERRIDE Keyword Using new keyword
C# supports method hiding. new keyword is used to explicitly hide member it inherited from base class.
Example
int i=7
Object o=i
Object o=i
Object 0=23
int i=(int)o
int i=(int)o
virtual keyword is used while implementing method overriding. method overriding stands for overriding method of base class with derived class.
Program.
using System;
namespace virtual_override
{
class Base
{
public virtual void show()
{
Console.WriteLine("Base class method");
Console.ReadKey();
}
}
class Derived:Base
{
public override void show()
{
Console.WriteLine("derived class method");
Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
// b.show();
d.show(); //will show derived class output
}
}
}
namespace virtual_override
{
class Base
{
public virtual void show()
{
Console.WriteLine("Base class method");
Console.ReadKey();
}
}
class Derived:Base
{
public override void show()
{
Console.WriteLine("derived class method");
Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
Base b = new Base();
Derived d = new Derived();
// b.show();
d.show(); //will show derived class output
}
}
}
derived class method
class Base
{
public void show()
{
Console.WriteLine("Base class method");
Console.ReadKey();
}
}
class Derived:Base
{
public new void show()
{
// base.show();
Console.WriteLine("derived class method");
Console.ReadKey();
}
}
{
public void show()
{
Console.WriteLine("Base class method");
Console.ReadKey();
}
}
class Derived:Base
{
public new void show()
{
// base.show();
Console.WriteLine("derived class method");
Console.ReadKey();
}
}
There are total five Access Specifiers in C Sharp.
Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers
Reviewed by vishal
on
July 31, 2017
Rating:
No comments