Friday, January 18, 2013

Some Mixed interview Topic


The default keyword.
The default keyword can be used in the switch statement or in generic code:

The switch statement: Specifies the default label.

Generic code: Specifies the default value of the type parameter. This will be null for reference types and zero for value types.
-------------------------
Fixed
The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context. Fixed can also be used to create fixed size buffers.
The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement. Without fixed, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a fixed statement.
----------------------
The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. In this case, the method must also be declared as static, as shown in the following example:
[DllImport("avifil32.dll")]
private static extern void AVIFileInit();
The extern keyword can also define an external assembly alias, which makes it possible to reference different versions of the same component from within a single assembly.
----------------
The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface. The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.
The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.
At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.
A foreach loop can also be exited by the goto, return, or throw statements.

For generic type parameters, the in keyword specifies that the type parameter is contravariant. You can use the in keyword in generic interfaces and delegates.
interface IContravariant<in A> { }
// Extending contravariant interface.
interface IExtContravariant<in A> : IContravariant<A> { }
------------------------
In C#, the new keyword can be used as an operator, a modifier, or a constraint.
new Operator
Used to create objects and invoke constructors.
new Modifier
Used to hide an inherited member from a base class member.
new Constraint
Used to restrict types that might be used as arguments for a type parameter in a generic declaration.
------------------
The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants
-------------
The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.
To compile unsafe code, you must specify the /unsafe compiler option. Unsafe code is not verifiable by the common language runtime.
--------------
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.
The volatile keyword can only be applied to fields of a class or struct. Local variables cannot be declared volatile.
---------------------
The stackalloc keyword is used in an unsafe code context to allocate a block of memory on the stack.
The keyword is valid only in local variable initializers.

10 comments:

  1. When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. You use an iterator to perform a custom iteration over a collection

    ReplyDelete
  2. You use a yield return statement to return each element one at a time.

    ReplyDelete
  3. public static void Process()
    {
    // Display powers of 2 up to the exponent of 8:
    foreach (int number in Power(2, 8))
    {
    Response.Write(number.ToString() + " ");
    }
    // Output: 2 4 8 16 32 64 128 256
    }


    public static IEnumerable Power(int baseNumber, int highExponent)
    {
    int result = 1;

    for (int counter = 1; counter <= highExponent; counter++)
    {
    result = result * baseNumber;
    yield return result;
    }
    }

    ReplyDelete

  4. BasicHttpBinding: If clients can be non-WCF. This exposes service to the outside world as if it was an ASMX (webservice). You cannot take advantages of modern WS* protocol on this binding. This is unsecure binding by default.

    NetMsmqBinding: If client is WCF application and requires offline or disconnected interaction choose NetMsmqBinding which uses MSMQ for transporting the messages.

    NetTcpBinding: If client requires connected communication and no problem on communicating between firewalls as it requires port to open for communication across firewalls. This is WCF to WCF communication.This is secure and optimized binding that is suitable for on machine communications between WCF applications.

    NetNamedPipeBinding (IPC Binding): It is similar to TCP protocol but if requirement is to communicate on same machine then you can consider it.

    WSHttpBinding: If you want a secure and interoperable binding that is suitable for non-duplex service contracts.

    WSDualHttpBinding (WS dual binding): It is similar to WS binding but it can also support duplex communication from service to client.It it nothing more than two WsHttpBinding bindins wired up against each other to support callbacks

    ReplyDelete
  5. Encapsulation-Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

    In other words, encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions).

    ReplyDelete
  6. Encapsulation and Inheritance are two key concepts in OOP that serve different purposes. While encapsulation hides inessential information, inheritance allows the object of a class to adopt the attributes of another class.

    When you implement inheritance in an application, the classes in the application are arranged in a strict hierarchy. The classes at the lower levels inherit the attributes of the classes at the higher levels.

    In addition to inheriting the characteristics of the parent class, a class may have its own attributes. This feature implies that the code and characteristics of a class are reusable and extensible. However, inheritance compromises encapsulation because a subclass can directly access the parent’s protected attributes without using operations.

    ReplyDelete
  7. The object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead, the data is accessed by calling specially written functions, commonly called methods, which are bundled in with the data. These act as the intermediaries for retrieving or modifying the data they control. The programming construct that combines data with a set of methods for accessing and managing those data is called an object.

    ReplyDelete
  8. Difference Between Static and singletons
    Static classes and singletons both provide sharing of redundant objects in memory, but they are very different in usage and implementation.


    Static Class - You can not create the instance of static class.
    Singleton pattern - you can create one instance of the object and reuse it.
    Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
    Singleton instance is created for the first time when the user requested.
    Static Class class cannot have constructor.
    singleton class can have constructor

    ReplyDelete
  9. We can dispose the objects of a singleton class but not of static class

    ReplyDelete
  10. we cannot pass the static class to method

    We cannot inherit Static class to another Static class in C#.

    ReplyDelete