Home > .NET Framework > Scott Hanselman’s interview questions – C# Component Developers

Scott Hanselman’s interview questions – C# Component Developers

Answers to C# Component Developers
  1. Juxtapose the use of override with new. What is shadowing?

    This calls for a separate entry http://sendhil.spaces.live.com/blog/cns!30862CF919BD131A!584.entry

  2. Explain the use of virtual, sealed, override, and abstract.

    Some of it is explained in the answer to the previous question
    Sealed when applied to classes prevents other developers from inheriting from them
    Please also note the application of sealed to methods as explained, Manu and Prakash pointed me to this.

  3. Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d It is a fully specified assembly name
    • Part 1: Foo.Bar, Name of the file containing the manifest (without extension)
    • Part 2: Version=2.0.205.0, Assembly version (Used to resolve references apply version policy etc)
    • Part 3: Culture=neutral, Assembly Culture (Used to specify assemblies for various cultures)
    • Part 4: PublicKeyToken=593777ae2d274679d, Assembly Publisher’s Public Key token (Used to verify if the assemblies have been tampered with after development, Also serves to differentiate assemblies from multiple publishers with the same name)

  4. Explain the differences between public, protected, private and internal.

    These are called access specifiers and they control type / member visibility.
    Let us see them one by one

    • Public: Type / Member is visible to everyone who uses the type.
    • Protected: Type / Member is visible to any other type which derives from this type.
    • Private: Type / Member is visible only within this type.
    • Internal: Type / Member is visible only within this assembly.
    • Protected Internal: Type / Member is visible only within this assembly and to Derived Types ouside the assembly.

    Generic Term 

    C#

    VB.NET

    Description

    Private

    private

    Private

    Type / Member is visible only within this type.

    Public

    public

    Public

    Type / Member is visible to everyone who uses the type.

    Family

    protected

    Protected

    Type / Member is visible to any other type which derives from this type.

    Assembly

    internal

    Friend

    Type / Member is visible only within this assembly.

    Family Or Assembly

    protected internal

    Protected Friend

    Type / Member is visible only within this assembly and to Derived Types ouside the assembly.

    Family And Assembly

    Type / Member is visible only to Derived Types within this assembly.

    Other trivia about acccess modifiers.
    The following other rules are enforced by the CTS
    Normal Types can have Public / Assembly access modifiers alone
    Nested Types can have All the access modifiers
    Members on interfaces cannot have any access modifiers, they are public by default.

  5. 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 (as metadata) of types implemented with COM. There can be only one primary interop assembly, which must be 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 your .NET app to interop with existing COM objects
    PIAs are a necessary evil in this scenario. Assume you have a COM Components C1. Both C1 refers another COM component C2. The type library importer will actually generate two assemblies: (1) an Interop assembly for C1 (2) an interop assembly for C2. Now assume you have a COM component C3 which also depends on C2. The type library importer will again generate two assemblies: (1) an Interop assembly for C3 (2) an interop assembly for C2. If you use both C1 and C3 in your application four Interop Assemblies are generated by the Type Library Importer, which is a problem. PIA’s come to the rescue here. If you have a PIA for C2 then the Type Library Importer will not generate the Interop Assembly which solves the problem.

  6. By what mechanism does NUnit know what methods to test?

    Attributes in .NET provide an elegant, consistent approach to adding declarative information to runtime entities. Because the runtime entities interact with the supporting services via declarative information, the set of services and supporting attributes does not have to be closed. By providing a standard mechanism to extend built-in metadata with custom attributes, .NET lets programmers develop applications that can interact with services not yet defined or supported by CLR. In fact, Nunit version 2.0 was written with custom attributes… From http://www.martinfowler.com/ieeeSoftware/netAttributes.pdf
    Attributes help to do Declarative Programming by extending metadata. Meaning, Attributes are not only used by the runtime (CLR). Attributes are useful for other tools as well. How can the NUnit GUI runner load only the unit tests. JUnit does this by starting the methods with "test", inheriting the test classes from a marker base class. NUnit does it more elegantly using attributes.

  7. What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}

    When you use throw e, the original stack trace is lost. When you use throw; the original stack trace is preserved.

  8. What is the difference between typeof(foo) and myFoo.GetType()?

    ??

  9. 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 base constructor

  10. What is this? Can this be used within a static method?

    this is the current instance pointer. It cannot be used in static methods as they do not have an instance associated with them.

Advertisements
Categories: .NET Framework
  1. Eugen
    March 13, 2008 at 4:37 pm

    8. What is the difference between typeof(foo) and myFoo.GetType()?
     
    typeof takes a type as a parameter whereas GetType() is applied on an object. Both will return an object type describing that type.

  2. Eugen
    March 13, 2008 at 4:40 pm

    Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?
     
    Actually this() calls the parameterless constructor not the base constructor. It is useful ’cause you don’t need to use a third method to call it in both c() and c(string)

  3. Eugen
    March 13, 2008 at 4:46 pm

    What is this? Can this be used within a static method?
     
    Well, it is an alias for the current instance, true. In .NET 3.0 this is also a keyword used in static methods called extension methods which allow calling the extension method as a member method of the type they are extending:
     
    class Example
    {
         public void F()
         {
              Console.WriteLine("Example.F() instance method called");
         }
    }
     
    static class ExampleExtension
    {
        public static void E(this Example obj)
        {
             Console.WriteLine("Example.E() extension method called");
        }
    }
     
    public static void Main()
    {
        Example e = new Example();
     
        e.F();
        e.E();
    }

  4. Unknown
    November 23, 2008 at 10:06 am

    您需要二手液晶显示屏、废旧液晶屏么?我们是不折不扣的二手液晶屏、旧液晶屏大批发商,长期大量供应可再利用的旧液晶屏。我公司提供的各种尺寸的二手液晶屏, 不同厚薄如笔记本屏,均已经过我们严格的分类,检验,测试流程。请访问协力液晶屏www.sceondhandlcd.com[bbibbefghhbbibdi]

  1. November 19, 2010 at 11:01 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: