tags:

views:

204

answers:

1

Recently I faced few interview questions.The interviewer asked the to give the detailed answer.

1)Can we override a WCF service (Its is not OOPS overriding) ?.Explain the reason on either end. (WCF Related).

2)Can we override Page events (Page_Load())?.Explain reason.(ASP.NET related).

3)What is the primary responsibility of Pre_Init( page) event ,apart from user preference setting,skinning?

4) Can we override Static methods.Explain the reason.(C# related)

can anyone help me to understand the reasons?

+2  A: 
  1. You can't really override WCF service operations. If your Service Contract class has two Service Operation methods with the same name but different parameters (i.e. legitimate C# overloads), WCF will throw an InvalidOperationException when the service is started. If you really want to do this, you can change the exposed operation name of one of the methods in the OperationContract attribute:

    [OperationContract(Name = "GetDataWithString")]
    public string GetData(string input)
    {
       ...
    }
    
    
    [OperationContract(Name = "GetDataWithNumber")]
    public string GetData(int input)
    {
       ...
    }
    
  2. You can override Page events in ASP.Net; this is pretty widely used and usually pretty crucial. You can either explicitly override the methods from the Page class your custom page inherits from, or you can name your methods in such a way that ASP.Net knows that they're to be treated as overrides. For example, declaring a method in a page's code-behind with the signature below will automatically override the Page_Init method.

    void Page_Init(object sender, EventArgs e)
    
  3. The Page_Init method is where ASP.Net starts tracking ViewState. This means that anything done to any of the page's controls is now marked as Dirty in the ViewState StateBag, and so will be base-64 encoded and sent down to the client in the ViewState hidden input field, and therefore sent back to the server on a postback. Changing controls' values before ViewState is being tracked will help stop ViewState getting too large. See this seminal article for more details.

  4. Only class instance methods can be marked as virtual, as the compiler-created v-table is attached to class instances. Static class members are not attached to instances but to the class itself, therefore there's no way to override them. This article explains this in more detail, and gives some workarounds.

Graham Clark