03 June 2005

What Great .NET Developers Ought To Know (Part Three C# Component Developers)

Continue on Scott Hanselman's - What Great .NET Developers Ought To Know
Part Three C# Component Developers

Juxtapose the use of override with new. What is shadowing?
Using override keyword to a method in the derived class meaning the method provides a new implementation to the overridden method (same signature) in the base class. The base method must be virtual, abstract, or override.
By default a method is not modified by ‘virtual’. So if a method in derived class wants to override the base implementation, it needs to be modified by ‘new’ keyword.
This is called shadowing, not overriding. The method in the derived HIDES the one in the base class.

Explain the use of virtual, sealed, override, and abstract.
virtual keyword is used to modify a method or property declaration. It allows the downcasting from base class to derived class. From derived class’s point, the method overrides it specialised the implementation. To enforce the overriding, base method modify with abstract.
abstract keyword is used to modify a class, method or property declaration. You cannot instantiate an abstract class or make calls to an abstract method directly.
An abstract virtual method meaning that the method provides maybe a partial implementation, may be no implementation at all. And a derived class of it must implement it.
A sealed class cannot be derived. This modifier is most likely used on a static class. An abstract class cannot be sealed.
A sealed override method in a derived class prevent it being further overriden.

Explain the importance and use of each component of this string: Foo.Bar, Version=, Culture=neutral, PublicKeyToken=593777ae2d274679d
This string specifies shows a fully specified reference for a strongly named assembly.

Explain the differences between public, protected, private and internal.
Visibility differences on accessing a method, class:
private methods are only visible to a member in the same class;
protected methods are only visible to members in the derived class plus private scope;
internal methods/classes are visible to members in the same assembly;
public methods/class are visible to all.

What benefit do you get from using a Primary Interop Assembly (PIA)?
You get compatibility between applications that shares types defined in a PIA.
PIA contains the official, unique type identity description as defined by the publisher of those types. The PIA may contain certain customizations that make the types easier to use from managed code. The PIA is always signed by the publisher of the original COM type.
By what mechanism does NUnit know what methods to test?

What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}
catch(Exception e){throw e;} suppress the original exception stack and begin a new exception flow. Original stack trace information maybe lost
catch(Exception e){throw;} re-throw original exception.

What is the difference between typeof(foo) and myFoo.GetType()?
Operatior typeof(foo) is used to obtain the System.Type object for a type at compile time.
myFoo.GetType() is used to obtain the exact run time type of an object. This method uses reflection.

Explain what’s happening in the first constructor and how is this construct useful?

public class c{
public c(string a) : this() {;};
public c() {;}

When constructor public c(string a) is called to construct an object, it first calls the default constructor then the initialisation procedures in public c(string a).

What is this? Can this be used within a static method?
The this keyword refers to the current instance of the class. It cannot be use within a static method, because static method doesn’t live in an instance object type.

No comments: