Casting and Reference Conversions – Differentiate between Upcasting and Downcasting

An object reference can be:
  • Implicitly upcast to a base class reference
  • Explicitly downcast to a subclass reference
Upcasting and downcasting between compatible reference types performs reference conversions: a new reference is (logically) created that points to the same object. 
An upcast always succeeds; a downcast succeeds only if the object is suitably typed.

Upcasting

An upcast operation creates a base class reference from a subclass reference. For example:

Vechicle oVechicle = new Vechicle();
Car oCar = oVechicle; // Upcast

After the upcast, variable oCar still references the same Vechicle object as variable oVechicle. The object being referenced is not itself altered or converted:

Console.WriteLine (oCar == oVechicle); // True

Although oCar and oVechicle refer to the identical object, oCar has a more restrictive view on that object:

Console.WriteLine (oCar.Name); // OK
Console.WriteLine (oCar.VechicleCount); // Error: VechicleCount undefined

The last line generates a compile-time error because the variable oCar is of type Car, even though it refers to an object of type Vechicle. To get to its VechicleCount field, you must downcast the Vechicle to a Car.

Downcasting

A downcast operation creates a subclass reference from a base class reference. For example:

Vechicle oVechicle = new Vechicle();
Car oCar = oVechicle; // Upcast

Vechicle oVechicle1 = (Vechicle)oCar; // Downcast
Console.WriteLine (oVechicle1.VechicleCount); // <No error>
Console.WriteLine (oVechicle1 == oCar); // True
Console.WriteLine (oVechicle1 == oVechicle); // True

As with an upcast, only references are affected—not the underlying object. A downcast requires an explicit cast because it can potentially fail at runtime:

Maruti oMaruti = new Maruti();
Car oCar = oMaruti; // Upcast always succeeds
Vechicle oVechicle = (Vechicle)oCar; // Downcast fails: oCar is not a Car

If a downcast fails, an InvalidCastException is thrown. This is an example of runtime type checking.

Explain the IEnumerable Interface

Namespace – System.Collections
Assembly mscorlib (in mscorlib.dll)

IEnumerable Interface is the base interface for all non-generic collections that can be enumerated. It exposes an enumerator, which supports a simple iteration over a non-generic collection. System.Collections.Generic.IEnumerable<T> is the generic version of this interface.

IEnumerable contains a single method, GetEnumerator, which returns an IEnumerator which provides the ability to iterate through the collection by exposing a Current property and MoveNext and Reset methods.

Implementation of IEnumerable and IEnumerator on collection classes enables the foreach syntax on a collection. The members of these interfaces are not explicitly called, but they are implemented to support the use of foreach to iterate through the collection.

Static Constructors - What is the output of the C# code?

class A
{
  public A(string text)
  {
    Console.WriteLine(text);
  }
}

class B
{
  static A a1 = new A("a1");

  A a2 = new A("a2");

  static B()
  {
    a1 = new A("a3");
  }

  public B()
  {
    a2 = new A("a4");
  }
}

class Program
{
  static void Main(string[] args)
  {
    B b = new B();
  }

}

Before any code of the type B gets executed, its static constructor will be invoked first. It initializes static data fields and then executes statements inside the static constructor. Therefore, it prints a line of “a1”, and then another line of “a3”.

When the execution flow reaches the statement B b = new B(), the ordinary constructor is invoked. It initializes its data fields first and then the statements in its constructor method, so it prints a line of “a2”, and then another line of “a4”.

Output:

a1
a3
a2
a4

Access Modifiers in C#

Access Modifiers are used to control the visibility of a type (classes, interfaces, structures, enumerations, and delegates) or their members (properties, methods, constructors, and fields) to other parts of an application.

Role of each access modifier and where it may be applied:

Access Modifier Applicable to Description
public Types or type members Public items have no access restrictions. A public member can be accessed from an object, as well as any derived class. It can also be accessed from other external assemblies.
private Type members or nested types Private items can only be accessed by the class (or structure) that defines the item.
protected Type members or nested types Protected items can be used by the class which defines it, and any child class. However, protected items cannot be accessed from the outside world using the C# dot operator.
internal Type or type members Internal items are accessible only within the current assembly. Therefore, if you define a set of internal types within a .NET class library, other assemblies are not able to make use of them.
protected internal Type members or nested types
When the protected and internal keywords are combined on an item, the item is accessible within the defining assembly, the defining class, and by derived classes.

Note: By default, type members (ex. members of a class) are implicitly private while types (ex. classes) are implicitly internal.

// An internal class with a private default constructor.
class Country
{
// default constructor – implicitly private
Country()
{
}
}

Note: Nested types can have private access, but non-nested types cannot be marked private.

public class Country
{
// OK! Nested types can be marked private.
private enum FlagColor
{
Red, Green, Blue
}
}

Here, it is permissible to apply the private access modifier on the nested type. However, non-nested types (such as the Country) can only be defined with the public or internal modifiers.

// Error! Nonnested types cannot be marked private!
private class Country
{}

static keyword in C#

When a class member is prefixed by a static keyword, it becomes a static member, and it must be invoked directly from the class level, rather than from an object reference variable. Static members promote a given item to the class level rather than the object level. Static data is allocated once and shared among all instances of the class. The CLR allocates the static data into memory exactly one time.

While any class can define static members, they are quite commonly found within utility classes. By definition, a utility class is a class that only exposes static functionality. It does not maintain any object-level state and is not created with the new keyword.
Rather, a utility class exposes all functionality as class-level (a.k.a., static) members.

The static keyword can be applied to data members, methods, properties, constructor, and entire class definition.

Note: this keyword cannot be used with a static member because this implies an object. A static member cannot reference non-static members in its implementation (it will generate a compiler error).

Q: What will happen if you attempt to assign the value of a static data member in a typical (non-static or instance level) constructor?

A: The the value of the static data member will be reset each time you create a new object.

Static Constructor: A static constructor is a special constructor that is an ideal place to initialize the values of static data when the value is not known at compile time (e.g., read in the value from an external file, a database, generate a random number, etc).

A static constructor allows us to initialize static members of a class at runtime. The CLR calls all static constructors before first use (and never calls them again for that instance of the application).

Few interesting points regarding static constructors:
  1. A given class can define only one static constructor. The static constructor cannot be overloaded.
  2. A static constructor does not take an access modifier and cannot take any parameters.
  3. A static constructor executes exactly one time, regardless of how many objects of the type are created.
  4. The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller.
  5. A static constructor cannot be called directly through the code.
  6. The static constructor executes before any instance-level constructors.
Static Class: A static class cannot be created using the new keyword (i.e; it cannot have an instance or object). It can contain only static members (static data members, static methods, static properties or a static constructor).

Endpoint in a WCF Service

A WCF service exposes its contract via collection of Endpoints (one or more endpoints). An endpoint in WCF is an entity which facilitates access of a client to a WCF service. All communication with the WCF service occurs through the endpoints exposed by the service.

In simple terms, it's an URL on which clients can communicate to the service.

An endpoint consists of the following four properties:
  1. Address – The Endpoint's Address is a network address where the Endpoint resides. It indicates the location of the endpoint, i.e.; where it can be found. The address uniquely identifies the endpoint.
    The address of an endpoint is represented in the WCF object model by the EndpointAddress class.
    An EndpointAddress class contains:
    1. A URI property which represents the address of the endpoint.
    2. An Identity property, which represents the security identity of the service and a collection of optional message headers. The optional message headers are used to provide additional and more detailed addressing information to identify or interact with the endpoint.
  2. Binding – The binding specifies how a client can communicate with the endpoint and includes:
    1. Transport protocol (ex – TCT or HTTP).
    2. Message encoding (text or binary).
    3. Security requirements (SSL or SOAP message security).
    A binding is represented in the WCF object model by the abstract base class Binding
  1. Contracts – The contract outlines the functionality exposed by the endpoint to the client. It specifies:
    1. The operations available for the client.
    2. The form of the message.
    3. The type of input parameters or data required to call the operation.
    4. The expected type of processing or response message for the client.
  2. Behaviors – The endpoint has a set of behaviors that specify local implementation details of the endpoint. The local behavior of the service endpoint can be customized by using endpoint behaviors (which achieves this by participating in the process of building a WCF runtime).

Endpoint in a WCF Service - Explained by Mr. Sandeep Karan



Endpoint in a WCF Service