C# Interview Questions III



What is the difference between shadow and override?

Overriding is used to redefines only the methods.but shadowing redefines the entire element.

 

You have an event handler called MyEvent and you want to link the click event of control, MyButton, to use MyEvent, what is the code that will like them together?

control.Click += new EventHandler(MyEvent)

 

Which debugging window allows you to see the methods called in the order they were called?

The call stack.

 

Which debugging window allows you to see all the name and values of all the variables in scope?

In Call stack window in MS Visual Studio IDE.

 

What is wrapper class?is it available in c#?

Wrapper Classes are the classes that wrap up the primitive values in to a class that offer utility method to access it . For eg you can store list of int values in a vector class and access the class. Also the methods are static and hence you can use them without creating an instance . The values are immutable .

 

What is protected internal class in C#

Class can be internal not protected or not even protected internal. Class can have only two access modifiers Public and internal.if any field or method having access modifier protected internal then this field available to the others assemblies class and as well as class whose inharits this class.

 

Which keyword is used of specify a class that cannot inherit by other class

By using “sealed” keyword we can restrict a class from inheritence.

 

Can u create the instance for abstract classes

No we cannot instanciate abstract class we hav to extend it first

ex-

abstract class A

{}

class B : A

{}

Class C

{

B b=new B;

}

 

Can we use Friend Classes or functions in C# the way we use it in C++

No we can not use friend keyword but instead c# provide “internal” keyword for friend

 

How we can use inheritance and polymorphisms in c# programming?

Inheritance in C# is similar to cpp, if you are familiar to cpp, the only difference is you dont inherit a class in public or protected mode, the way to inherit a class is Class Base { protected int length, breadth; public Base(int a, int b) { this.length = a; this.breadth = b; } public virtual int Show() { Console.WriteLine(Length of a rectangle is {0} and breadth is {1}, length, breadth); } } Class Derived: Base // The way to inherit a class in C# { public Derived(int l, int b, string color): base(l, b) // call Base constructor { this.color = color; } // an overridden method beacuse in the derived method we can // change the behaviour, This is a kind on polymorphism. public override int Show() { base.Show(); Console.WriteLine(”Color of the rectangle is ” color); } public string color; } Class Tester { public static void Main() { Base b = new Base(10, 20); Derived d = new Derived(30, 40, “Red” ); b.Show(); d.show(); } }Output: Length of a rectangle is 10 and breadth is 20 Length of a rectangle is 30 and breadth is 40 Color of the rectangle is RedI hope this will make you a bit clear about Inheritance and polymorphism.



Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: