Q46. What is virtual keyword in C#? describe around virtual, override and new keyword in C#.
Q47. Can you access private variables in child class?
Q48. How you handle exceptions in your application? on server side.
Q49. Suppose you have a base class and a child class. Now while creating an instance of a child class which constructor will be called first?
Q50. Difference between Public, private, protected and Internal in C#.
======================================================================
Q46. What is virtual keyword in C#? describe around virtual, override and new keyword in C#.
Answer:
Virtual and override keyword are used for method over riding and new keyword is used for method hiding.
In below example. one can see how overriding works with and without virual keyword.
Good example
Class A
{
public void Test() { Console.WriteLine("A::Test()"); }
public virutal void BEST() { Console.WriteLine("A::BEST()"); }
}
Class B:A
{
public void Test() { Console.WriteLine("B::Test()"); }
// public new void Test() { Console.WriteLine("B::Test()"); }
// use the keyword new if a.Test(); // output --> "A::Test()" is the intended output.
// C# will not throw error assuming new itself. but will give warning for use of new.
public OVERRIDE void BEST() { Console.WriteLine("B::BEST()"); }
}
Main()
{
A a = new A();
B b = new B();
a.Test(); // output --> "A::Test()"
b.Test(); // output --> "B::Test()"
a = new B();
a.Test(); // output --> "A::Test()" // if this is intended output then we have to use keyword "new"
a.BEST() // output --> B::BEST()
}
======================================================================
Q47. Can you access private variables in child class?
Answer:
No
======================================================================
Q48. How you handle exceptions in C#?
Answer.
By following tricks
1. Try-catch-Finally blocks.
In Catch block we can have logging for auditing purpose.
In Finally block we should release any resource that might have opened in try block.
2. Exception filters. We can fine tune our catch block with type of filters and take different action based on different type of expetion.
3. Use of Throw word. We can throw our own exception if we want.
4. We can create user defined exceptions as well. we can create a class and inherit exception class in that class.
5. Do logging to track exceptions.
======================================================================
Q49. Suppose you have a base class and a child class. Now while creating an instance of a child class which constructor will be called first?
Answer:
Class A { ctor{ console.write("A Constructor")}};
Class B: A { ctor {Console.write ("B Constructor")}}
B objb = new B(); // Ctor of A will be called first then B
A objab = new B(); // Ctor of A will be called first again then B
======================================================================
Q50. Difference between Public, private, protected and Internal in C#.
Answer:
All these are access modifiers.
- public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
- private: The type or member can be accessed only by code in the same class or struct.
- protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
- internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
- protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
- private protected: The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
======================================================================
No comments:
Post a Comment