Friday, 4 January 2008

.NET Interview Questions (4)


C# Component Developers


· Juxtapose the use of override with new. What is shadowing?
Override modifier is used to a method signature in a derived class to provide a new implementation to the same method signature in the base class. The base method must have a modifier override, virtual or abstract.
If there is no such modifier in the base class method, then the method is not overridable. To provide a new implementation in the derived class, a “new” modifier is required. This is called shadowing. The implementation of derived class hide the implementation of base class.

· Explain the use of virtual, sealed, override, and abstract.
Virtual – a method with virtual modifier can be overridden in a derived class.
Sealed – a sealed class cannot be derived.
Override – a method with override modifier provide a new implementation of a same method name in the base class.
Abstract – an abstract class cannot be instantiated and must be inherited. An abstract method or property cannot be used directly, they must be overridden in derived class.

· Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d
The string shows a reference to a strongly named assembly.
Foo.Bar –Filename without extension, file containing the manifest.
Version – assembly version, used to resolver version problem
Culture – assembly culture, used to specify assemblies for various cultures.
PublicKeyToken – used to verify if assemblies have been tampered after publishing. And to differentiate assemblies from multiple publishers with the same name.

· Explain the differences between public, protected, private and internal.
Public – class or method is accessible to everyone who use it.
Protected – class or method is visible to derived class.
Private – class or method is visible to other members within the same class.
Internal – class or method is visible to other members within the same assembly.
Protected Internal – class or method is visible to member with the same assembly and to derived classes outside the assembly.

· What benefit do you get from using a Primary Interop Assembly (PIA)?
A primary interop assembly is a unique, vendor-supplied assembly that contains type definitions of types implemented with COM. There is only one primary interop assembly, which is signed with a strong name by the publisher of the COM type library.
A single primary interop assembly can wrap more than one version of the same type library. A COM type library that is imported as an assembly and signed by someone other than the publisher of the original type library cannot be a primary interop assembly. Only the publisher of a type library can produce a true primary interop assembly, which becomes the unit of official type definitions for interoperating with the underlying COM types. PIAs provide a single place for.NET apps to interop with existing COM objects.

· By what mechanism does NUnit know what methods to test?
By using [Test] attribute to decorate a method.

· What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}
Throw e – the original stack trace is lost.
Throw - the original stack trace is kept.

· What is the difference between typeof(foo) and myFoo.GetType()?
Typeof – get the object type at compile time.
GetType() – get the object type at runtime using reflection.

· Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?
The first constructor calls base constructor.

· What is this? Can this be used within a static method?The this is the reference to the current instance of an object. This keyword cannot be used in a static method because it doesn’t have an instance.

1 comment:

Arthur Medzhitov said...

Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?

The first constructor calls the second constructor of the same class . Calling the base constructor would be:
public class c{ public c(string a) : base() {;}