Saturday 18 March 2017

C# basic interview questions Part-8

1. Ques: What is the Environment Class in C#? 
Answer:
The System.Environment Class provides information about the current environment and platform. The System.Environment Class uses to retrieve Environment variable settings, Version of the common language runtime, contents of the call stack etc. This class cannot be inherited.

The Environment class use for getting and setting various operating system related information. You can use this class to retrieve information such as command-line arguments, exit codes, environment variable settings, contents of the call stack, time since last system boot in milliseconds (tick count), and version of the CLR.


2. Ques: What is an Exception in C#?
Answer:
 An exception is an error that occurs during program execution. Generally, an exception describes an unexpected event.

  • The advantage of using exceptions is that the program doesn’t terminate due to the occurrence of the exception.
  • Whenever an exception is occurred the .NET runtime throws an object of specified type of Exception.
  • The class ‘Exception’ is the base class of all the exceptions.

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.

Here are a few common types of exceptions:

System.NullReferenceException Handles errors generated from deferencing a null object.
System.DivideByZeroException Handles errors generated from dividing a dividend with zero.
System.InvalidCastException  Handles errors generated during typecasting.


3. Ques:  What is the difference between system exceptions and application ?
Answer:
 System.SystemException -> This class is for exceptions that r usually thrown by the .net run time, or which r considered to be of a generic nature and must be thrown by almost any application. For example, StackOverflowException will be thrown by the .net run time if it detects the stack is full.

 System.ApplicationException-> This class is important, because it is the intended base for any class of exception defined by third parties. Hence, if u define any exceptions covering error conditions unique to your application, u should derive these directly or indirectly from System.ApplicationException.


4. Ques:  What is the difference between object pooling and connection pooling ?
Answer:
  • object pool implementation will increase the size of the pool up to the specified maximum. When the maximum is reached, unlike the example above, instead of throwing an exception, it makes the client wait until an object is returned to the pool, and then gives the newly returned object to the waiting client.
  • Connection pool implementation does not have a maximum size parameter - it will keep creating new connections to meet demand (eventually it will hit some system limit and the request will fail in some way that isn't specified).
  • With object pooling, you maintain a pool of objects in memory.
  • Connection pooling does the same thing with connections to a database.

5. Ques:  What is Extension methods in C# ? What is Benefits of extension methods ?
Answer:
Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special kind of static method, but they are called as if they were instance methods on the extended

An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

Important points for the use of extension methods are given as
  • An extension method must be defined in a top-level static class.
  • An extension method with the same name and signature as an instance method will not be called.
  • Extension methods cannot be used to override existing methods.
  • The concept of extension methods cannot be applied to fields, properties or events.
  • Overuse of extension methods is not a good style of programming.
Benefits of extension methods
  • Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code.
  • If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.
  • This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.

6. Ques:  What is Marshalling?
Answer:
Marshaling is the process of creating a bridge between managed code and unmanaged code; it is the homer that carries messages from the managed to the unmanaged environment and reverse. It is one of the core services offered by the CLR (Common Language Runtime.)


7. Ques:  What is difference between Serialization and Marshaling ?
Answer:
Serialization
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection to be "resurrected" later in the same or another computer environment. And this sequence of bits can be of any format the user chooses; however, they are usually formed as XML or binary.

Serialization comes in many forms in .NET Framework, it can be observed in ADO.NET, Web services, WCF services, Remoting, and others.

For example, calling the WriteXml() function of a DataSet serializes this DataSet into a XML file.

ds.WriteXml("data.xml");

Marshaling
Marshaling is the process of converting managed data types to unmanaged data types. There're big differences between the managed and unmanaged environments. One of those differences is that data types of one environment is not available (and not acceptable) in the other.

For example, you can't call a function like SetWindowText() -that sets the text of a given window- with a System.String because this function accepts LPCTSTR and not System.String. In addition, you can't interpret (handle) the return type, BOOl, of the same function, that's because your managed environment (or C# because of the context of this writing) doesn't have a BOOL, however, it has a System.Boolean.


8. Ques:  What are the New Features in C# 6.0 ?
Answer:
The New Features in C# 6.0 listed bellow
  • using Static.
  • Auto property initializer.
  • Dictionary Initializer.
  • nameof Expression.
  • New way for Exception filters.
  • await in catch and finally block.
  • Null – Conditional Operator.
  • Expression – Bodied Methods
  • Easily format strings – String interpolation

9. Ques:  What is Dynamic language runtime (DLR) in Dot net ?
Answer:
The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .Net Framework and to add dynamic features to statically typed languages."

The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages.
Dynamic languages can identify the type of an object at run time, whereas in statically typed languages such as C# and Visual Basic (when you use Option Explicit On) you must specify object types at design time. Examples of dynamic languages are Lisp, Smalltalk, JavaScript, PHP, Ruby, Python, ColdFusion, Lua, Cobra, and Groovy.

Advantages of dynamic languages 
  • The ability to use a rapid feedback loop (REPL, or read-evaluate-print loop). This lets you enter several statements and immediately execute them to see the results.
  • Support for both top-down development and more traditional bottom-up development. For example, when you use a top-down approach, you can call functions that are not yet implemented and then add underlying implementations when you need them.
  • Easier refactoring and code modifications, because you do not have to change static type declarations throughout the code.

10. Ques: What is type-safe in .net?
Answer:
Type-safe code accesses only the memory locations it is authorized to access. For example, type-safe code cannot read values from another object's private fields. It accesses types only in well-defined, allowable ways.

During just-in-time (JIT) compilation, an optional verification process examines the metadata and Microsoft intermediate language (MSIL) of a method to be JIT-compiled into native machine code to verify that they are type safe. This process is skipped if the code has permission to bypass verification

Verification of type safety is not mandatory to run managed code, type safety plays a crucial role in assembly isolation and security enforcement. When code is type safe, the common language runtime can completely isolate assemblies from each other. This isolation helps ensure that assemblies cannot adversely affect each other and it increases application reliability.

No comments:

Post a Comment

Thank you for comment