Category Archives: Interview Questions

Interview Questions

What is WCF Reliable Messaging?

WCF Reliable Messaging makes web services more reliable. It addresses the following problems:

  • A call to a web service does not make because the networks are not perfectly reliable as they suffer congestion and may drop signals.
  • OR the service response does not make it back to the client.

If I make multiple calls to the web service and the calls do not reach to the service in the same order that i made them there is a problem.

WCF reliable messaging makes sending messages reliable and ordered over unreliable network.

Demonstration:

Client: Hello Service!
Service: What may I help you!
Client: Message#1
Service: Message#1 received!
Client: Message#2
[No response from Service]
Client: Message#2
Service: Message#2 received!
Client: Good bye!
Service: Bye!

Explanation: The Client did not get a response when it send message#2. The reason may be the service did not get the message#2 or the service got the message but the response did not make it back. Whatever the reason, The client tries again and send the Message#2. if the service received the Message#2 twice it will ignore the second message.

@Html.Action vs @Html.RenderAction in Asp.Net MVC

@Html.Action

  • @Html.Action returns string.
  • Syntax : @Html.Action(“Action”,”Controller”).
  • Pass parameter with Action method Syntax : @Html.Action(“Action”,”Controller”, new {ParameterName=ParameterValue}).
  • It is slower as compared to @Html.RenderAction.
  • It’s result can be manipulated before rendering to the output stream.
  • Manipulation : @{ var data= Html.Action(“Action”,”Controller”, new {ParameterName=ParameterValue}); } @data I am Software Developer.
  • Use when data length is small or need to manipulate result before rendering to the output stream.

@Html.RenderAction

  • @Html.RenderAction returns void.
  • Syntax : @{ Html.RenderAction (“Action”,”Controller”); }
  • Pass parameter with RenderAction method Syntax : @{ Html.RenderAction (“Action”,”Controller”, new {ParameterName=ParameterValue});}.
  • It is faster as compared to @Html.Action because this result is written directly to the output stream.
  • It’s result can not be manipulated.
  • Use when data length is large or no need to manipulate result before rendering to the output stream.

@Html.Partial vs @Html.RenderPartial in Asp.Net MVC

@Html.Partial

  • @Html.Partial returns string.
  • Syntax : @Html.Partial(“Partial View Name”).
  • Pass model with Partial method Syntax : @Html.Partial(“Partial View Name”, Model).
  • It is slower as compared to @Html.RenderPartial.
  • It’s result can be manipulated before rendering to the output stream.
  • Manipulation : @{ var data= Html.Partial(“Partial View Name”); } @data I am Software Developer.
  • Use when data length is small or need to manipulate result before rendering to the output stream.

@Html.RenderPartial

  • @Html.RenderPartial returns void.
  • Syntax : @{ Html.RenderPartial(“Partial View Name”); }
  • Pass model with RenderPartial method Syntax : @{ Html.RenderPartial(“Partial View Name”, Model); }
  • It is faster as compared to @Html.Partial because this result is written directly to the output stream.
  • It’s result can not be manipulated.
  • Use when data length is large or no need to manipulate result before rendering to the output stream.

Soap vs Rest API

Soap

  • SOAP stands for Simple Object Access Protocol.
  • SOAP is a protocol.
  • SOAP uses HTTP and SMTP transport protocol.
  • SOAP supports only XML format.
  • Using HTTP, SOAP model can tunnel to across firewalls and proxies without any modification to the SOAP protocol.
  • SOAP is more secure as compare to REST because it uses own security model.
  • SOAP supports WS-Security and ACID transactions.
  • Sending message using XML is costly in terms of actual data being sent.

REST

  • REST stands for Representational State Transfer.
  • Rest is an architectural style.
  • REST uses HTTP transport protocol.
  • REST support Text, JSON, XML formats.
  • REST is completely stateless means it treat each web request independently.
  • Rest does not store information of the previous request.
  • Restful services provide a good caching mechanism over HTTP GET method. This can improve the perfoŕmance if the data does not change frequently.
  • REST uses uniform interface to interact with all the components.
  • REST inherit security from underlying transport (Http, Https).
  • Sending message using JSON is cheaper in terms of actual data being sent.

 

What are events in C#?

Events

  • Events is a way for a class to provide notifications to clients of that class when some interesting things happens to an object.
  • Events are declared using delegates.
  • Events can only be invoked from within the class that declared them.
  • Derived classes can not directly invoke events declared within the base class.
  • To call events in derived class, create a virtual protected invoking method for the event and override it in derived class.
  • An event can be placed in a interface.
  • When implementing the interface, the implementing class must supply a corresponding event in the class.

How to call child action method in Asp.Net MVC.

Child action methods can not be called directly. They have to call from inside of a view.
We can make an Action method a child action method by decorating it with [ChildActionOnly] attribute.

[ChildActionOnly]
public ActionResult Child()
{   
    return View();
}


Calling child action method from inside of a view

@Html.Action("Child", "Home")


Where “Child” is the Action method name and “Home” is the controller name.

When to use var instead of type in C#.

Example 1

var i=10;
int i=10;


Here it is an optional to declare i as var type. Because we know the value is integer type.

Example 2

string[] fruits = { "mango", "apple", "grape", "banana" };
foreach (var s in fruits)
{
Console.WriteLine(s);
}
//OR
foreach (string s in fruits)
{
Console.WriteLine(s);
}


it is also an optional to use var instead of string. Because we know all elements are string.

Example 3
Use var when we have to deal with anonymous type data.

var anonArray = new[] { new { FirstName = "Dev", Age = 24 }, new { FirstName = "Vikas", Age = 21 } };

foreach (var arr in anonArray)
{
    Console.Write(arr.FirstName);
    Console.WriteLine(arr.Age);
}
Console.ReadKey();

Difference between Abstraction and Encapsulation.

Abstraction

  • Abstraction means hiding implementation.
  • Abstraction can be achieved by interfaces or abstract classes.
  • Abstraction focuses on the outside view of an object (i.e. the interface)
  • TV Remote is the real world example of abstraction where we know little bit how to operate it. but does not know how they are implemented internally.

Encapsulation

  • Encapsulation means hiding the important features of a class which is not been needed to be exposed outside of a class and exposing only necessary things of a class.
  • Encapsulation can be achieved by Access modifiers (Public, Private, Protected).
  • Encapsulation focuses on the inside view of an object where it is actually implemented(i.e the class).
  • TV contains multiple things inside it – Color tube, connections of Speaker, Channel mechanism. is the real time example of encapsulation where these things does not need to be exposed outside.

Why multiple inheritance of classes is not supported in C#?

Multiple inheritance of classes is not supported in C# because of the diamond problem.
Diamond Problem: If in a scenario there is a Class A have a Print method. And another classes B and C is overriding Class A Print method with single inheritance concept. Now we have a class D which inherit both these classes B and C with multiple inheritance concept.

Diamond Problem

Diamond Problem

Example :

class A
{
    public virtual void Print()
    {
        Console.Write("Print A");
    }
}

class B : A
{
    public override void Print()
    {
        Console.Write("Print B");
    }
}

class C : A
{
    public override void Print()
    {
        Console.Write("Print C");
    }
}

class D : B, C
{        
}


Now when we will create the object of Class D and call the Print method. Compiler will be confused which parent class method to call. Hence multiple inheritance of classes is not supported in C#.