What is type casting. Explain it with reference to classes.

Answers were Sorted based on User's Feedback



What is type casting. Explain it with reference to classes...

Answer / kiran vaidya

Type casting means converting some type to some another
type.

for example, there are three classes:

class Employee
{
//Logic for this class

}

class Manager:Employee
{
//Logic for this class

}

class Clerk:Employee
{
//Logic for this class

}


class Program
{
static void Main(string [] args)
{
Employee e=new Employee();


//Type casting
Manager m=(Manager)e;

//Or
Clerk c=(Clerk)e;

}

}

Is This Answer Correct ?    13 Yes 2 No

What is type casting. Explain it with reference to classes...

Answer / shubha

Type Casting is conversion of one type data to another type.

Ex:
public class Base{
//Your code goes here
}

public class Derived extends Base
{
//Inherit method or create your own code here
}

public class MainClass {
public static void main(String args[]){

Base b = new Derived(); //reference variable of Base class points object of Derived class
//Derived d = b; compile time error, requires casting
Derived d = (Derived) b; // type casting Base to Derived
//Here you call your base class methods as derived class methods
}
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Sharp Interview Questions

What is the difference between TypeOf, GetType and what are the uses of TypeOf, GetType.

2 Answers   Siemens, Syntel,


How do you escape in c#?

0 Answers  


Is string reference type / value type?

56 Answers   Accenture, Microsoft, Siemens,


What is difference between .net and c#?

0 Answers  


What is ControlBox Propertie

0 Answers   MCN Solutions,






What is multicast delegate in c# ?

0 Answers  


Name the method of servicebase class?

0 Answers  


What is asenumerable in c#?

0 Answers  


What's the difference between an integer and int?

0 Answers  


What is a int in c#?

0 Answers  


What is difference between static and readonly in c#?

0 Answers  


How to declares a two-dimensional array in C#?

0 Answers   DELL,


Categories