Sunday, July 1, 2012

Use of System.Environment class in C# .NET


What is the use of System.Environment class in C#.NET?


The System.Environment class can be used to retrieve information like below :

  • Command-line arguments.
  • The exit code.
  • Environment variable settings.
  • Contents of the call stack.
  • Time since last system boot.
  • The version of the common language runtime.


Prevent class from being inherited in C#.NET


How to prevent a class from being inherited in C#.NET?


The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.


Add ReadOnly property in C#.NET?


How to add a ReadOnly property in C#.NET?


Properties can be made read-only by having only a get accessor in the implementation. For example


public class X
{
          public X(int id)
          {
               x_id = id;
          }
          public int ID
          {
              get
              {
                   return x_id;
              }
          }
}

Implementation inheritance and interface inheritance

What are implementation inheritance and interface inheritance?


Implementation inheritance is achieved when a class is derived from another class in such a way that it inherits all its members.


Interface inheritance is when a class inherits only the signatures of the functions from another class.

C# interview questions with answers, Part 3


C# developer interview questions and answers.


What is the difference between Finalize() and Dispose()?


Dispose() is called by as an indication for an object to release any unmanaged resources it has held. 
Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object. 
Dispose() operates determinalistically due to which it is generally preferred.



What is the difference between Debug.Write and Trace.Write? When should each be used?


Debug.Write: Debug Mode, Release Mode, It used while debuging a project.
Trace.write: Release Mode, It used in Released verion of Applications.



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


The virtual keyword enables a class to be overridden. If it has to be prevented from being overridden, then the sealed keyword needs to be used. If the keyword virtual is not used, members of the class can even then be overridden. However, its usage is advised for making the code meaningful.


The override keyword is used to override the virtual method in the base class. Abstract keyword is used to modify a class, method or property declaration. You cannot instantiate an abstract class or make calls to an abstract method directly.


An abstract virtual method means that the definition of the method needs to be given in the derived class.



What benefit do you get from using a Primary Interop Assembly (PIA)?


A primary interop assembly contains type definitions (as metadata) of types implemented with COM. Only a single PIA can exist, which needs to be signed with a strong name by the publisher of the COM type library.
One PIA can wrap multiple versions of the same type library.


A COM type library imported as an assembly can be a PIA only if it has been signed and published by the same publisher.


Therefore, only the publisher of a type library can produce a true PIA, that can be considered as the unit of an official type definition for interoperating with the underlying COM types.



Explain the use of static members with example using C#.NET.


Static members are not associated with a particular instance of any class. 
They need to be qualified with the class name to be called. 
Since they are not associated with object instances, they do not have access to non-static members.
i.e.: "this" cannot be used, which represents the current object instance.