Can you allow a class to be inherited, but prevent a method from being overridden in C#?
Yes. By Declaring the class public and making the method sealed.
Can you declare an overridden method to be static if the original method is not static?
No. Two virtual methods must have the same signature.
Can you inherit private members of a class?
No, we cannot inherit private members of a class because private members are accessible only to that class and not outside that class.
Is it possible to execute two catch blocks?
No. Whenever, an exception occurs in your program, the correct catch block is executed and the control goes to the finally block.
Can you prevent a class from overriding?
Yes. You can prevent a class from overriding in C# by using the sealed keyword; and in VB by using NotInheritable keyword.
What is 'this' pointer?
'this' pointer refers to the current object of a class.
Can 'this' be used within a static method?
No.
Is it possible for a class to inherit the constructor of its base class?
No. A class cannot inherit the constructor of its base class.
Can you inherit private members of a class?
No. You cannot inherit private member of a class because private members are accessible only to that class and not outside that class.
What's the difference between the 'ref' and 'out' keywords?
An argument passed as "ref" must be initialized before passing to the method whereas "out" parameter needs not to be initialized before passing to a method, and it will be initialized inside the method.
Difference Between Const, ReadOnly and Static ReadOnly in C# ?
Constant variables are declared and initialized at compile time. The value can't be changed after words.
Readonly variables will be initialized only from the non-static constructor of the same class.
Static Readonly variables will be initialized only from the static constructor of the same class.
What is the difference between “dispose” and “finalize” variables in C#?
Dispose() Method
- Dispose method will be used to free unmanaged resources like files, database connection etc.
- To clear unmanaged resources we need to write code manually to raise dispose() method.
- This Dispose() method belongs to IDisposable interface.
- If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.
Finalize() Method
- Finalize method also free unmanaged resources like database connections, files etc.
- It is automatically raised by garbage collection mechanism whenever the object goes out of scope.
- This method belongs to object class.
- We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process done.
Why to use "using" statement in C#?
The using block is used to obtain a resource and use it and then automatically dispose off when the execution of block completed.