This part will address the questions from section “C# Component Developers“.
== Juxtapose the use of override with new. What is shadowing? ==
override keyword is used to provide the implementation for an abstract method or a new implementation for a virtual method. The implementation will be accessible from the base type. This keyword can be used only on methods that are marked with abstract, virtual or override.
new keyword is used to provide a new implementation (method hiding or shadowing) that is only visible on the current type. The implementation will not be accessible from the base type. To access the previous implementation an up-cast would have to be performed. This keyword can be used on any methods (virtual, non virtual, abstract, non abstract etc.)
Shadowing is the process of hiding the base class’ implementation. The base class will not have access to the derived method. The new implementation will only be accessible on the type in which it got defined.
== Explain the use of virtual, sealed, override, and abstract ==
virtual keyword is used to mark methods, properties or events that can be overridden. It can be used on public and protected methods. If a method or property is private, it is not accessible by the derived types and it does not make sense to mark it virtual. Hence, virtual cannot be used on private methods.
sealed keyword is used to mark classes that we want to prevent other classes from deriving from (equivalent to the keyword final in Java)
override keyword was explained in the previous question
abstract keyword can be used on classes, methods, properties and events. If applied on a class then that class cannot be instantiated on its own, if applied on a method then that method cannot have any implementation in the class and the derived class is expected to provide the implementation.
an abstract function in C# is the equivalent of a pure virtual function in C++
== Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d ==
The .NET runtime identifies assemblies by the assembly’s fully qualified name that contains the mentioned components.
Foo.Bar is the assembly name that helps in identifying the assembly by name.
Version is the version of the assembly.
Culture is the language and regional conventions such as sorting, date time formats.
PublicKeyToken is a hash value corresponding to the private key that was used to sign the assembly. This value helps to make the assembly names unique in cases where the assemblies have the same file names.
== Explain the differences between public, protected, private and internal ==
public, protected, private and internal are access modifiers.
public – anyone can access the methods, property etc.
protected – the containing type and anyone that derives from it can have access
private – only the containing type can access the method, property etc.
internal – anyone within the same assembly can access the method or property. If the type is instantiated from a different assembly then types from outside that assembly will not be able to access the method or property etc.
More can be found in the docs.
== By what mechanism does NUnit know what methods to test? ==
NUnit just like most other frameworks does not care which methods are tested. It provides a framework to run unit tests. It finds those unit tests by examining attributes on both classes (looks for
) and methods (looks for methods with TestAttribute) and invokes them.== What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;} ==
The first exception handling code makes the stack trace start from there.
The second case ( throw; ) rethrows the existing exception leaving the exception’s stack trace intact.
Typically, the second case is the correct way to rethrow. When you see code such as “throw e;”, it is quite likely that whoever wrote the code had no clue what they were doing.
== What is the difference between typeof(foo) and myFoo.GetType()? ==
Both return the same: System.Type. The first one is resolved at compile time (we have to know the type upfront) and the second one is resolved at run time.
== Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful? ==
In the first constructor (with a parameter), first the parameterless constructor will be called and then the actual code from the first constructor.
This construct is useful when we have initialization logic that must be invoked no matter which constructor is used. In other words, it is useful for running common constructor logic.
== What is this? Can this be used within a static method? ==
this keyword refers to the current instance of the type. It allows to access any accessible instance variables. It is not available within static methods.
this keyword is also used when defining extension methods.
We will also use this keyword when we want to define an indexer on a class:
public string this[int index] { get { ... } set { ... } };