神刀安全网

C# Interview Questions for Experienced professionals (Part -1)

1. How will you specify what version of the framework your application is targeting?

You can define “targetFramework” in Web.config file to specify the framework version. It is introduced in Asp.net 4.0

<?xmlversion="1.0"?> <configuration> <system.web> <compilationtargetFramework="4.0" /> </system.web> </configuration> 

2. What is the difference between Static class and Singleton instance?

– In c# a static class cannot implement an interface. When a single instance class needs to implement an interface for some business reason or IoC purposes, you can use the Singleton pattern without a static class.

– You can clone the object of Singleton but, you can not clone the static class object

– Singleton object stores in Heap but, static object stores in stack

– A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded

3.What is the difference between Unicode,UTF, ASCII and ANSI code format of encoding?

Character encoding interprets the zeros and ones into real characters.

Unicode:

Unicode is a standard that can handle characters for almost all modern languages and even some ancient languages at the same time, as long as the client has fonts for the particular language installed in his system

UTF:

Unicode assigns each character a unique number, or code point. It defines two mapping methods, the UTF (Unicode Transformation Format) encodings, and the UCS (Universal Character Set) encodings. Unicode-based encodings implement the Unicode standard and include UTF-8, UTF-16 and UTF-32/UCS-4. They go beyond 8-bits.

ASCII:

ASCII is abbreviated from American Standard Code for Information Interchange, is a character encoding standard .ASCII codes represent text in computers.It is a code for representing characters as numbers, with each letter assigned a number from 0 to 127.In an ASCII file, each alphabetic, numeric, or special character is represented with a 7-bit binary number

ANSI:

ANSI is abbreviated from American National Standards Institute,is a character encoding standard.ANSI includes all the ASCII characters with an additional 128 character codes. ASCII just defines a 7 bit code page with 128 symbols. ANSI extends this to 8 bit and there are several different code pages for the symbols 128 to 255.

4. Can you serialize hashtable and Why?

No, You can’t Serialize Hash table.Because, the .NET Framework does not allow serialization of any object that implements the IDictionary interface

5. What is .PDB file?

PDB is an abbreviation for Program Data Base. It is a repository (persistant storage as databases) to maintain information required to run your program in debug mode. It contains many important relevant information required while debugging your code; for e.g. at what points you have inserted break points where you expect the debugger to break in Visual Studio etc..

6. Why singleton pattern is considered an Anti-pattern ?

– Singletons aren’t easy to handle with unit tests. You can’t control their instantiation and they may retain state across invocations.

– Memory allocated to an Singleton can’t be freed.

– In multithreaded environment, access to the singleton object may have to be guarded (e.g. via synchronization).

– Singletons promote tight coupling between classes, so it is hard to test

7. How to use Nullable Types in .NET?

Nullable types allow you to create a value type variable that can be marked as valid or invalid so that you can make sure a variable is valid before using it. Regular value types are called non-nullable types.

The important things you need to know about nullable type conversions are the following:

• There is an implicit conversion between a non-nullable type and its nullable version. That is, no cast is needed.• There is an explicit conversion between a nullable type and its non-nullable version.

For example, the following lines show conversion in both directions. In the first line, a literal of type

int is implicitly converted to a value of type int? and is used to initialize the variable of the nullable type.

In the second line, the variable is explicitly converted to its non-nullable version.

int? myInt1 = 15; // Implicitly convert int to int? int regInt = (int) myInt1; // Explicitly convert int? to int 

8. What are extension methods and where can we use them?

Extension methods enables you to add new capabilities to an existing type. You don’t need to make any modifications to the existing type, just bring the extension method into scope and you can call it like a regular instance method.Extension methods need to be declared in a nongeneric, non-nested, static class.

Notes:

  • The difference between a regular static method and an extension method is the special this keyword for the first argument.
  • Extension method cannot be declared on a class or struct.
  • It can also be declared on an interface (such as IEnumerable ). Normally, an interface wouldn’t have any implementation. With extension methods, however, you can add methods that will be available on every concrete implementation of the interface
  • Language Integrated Query (LINQ) is one of the best examples of how you can use this technique to enhance existing code.

You can read the implementation of Extension Methods in C#here.

9. How to implement singleton design pattern in C#?

There are several ways for implementing Singleton in C#.

  • Standard Implementation
  • Double checked locking
  • Early Instance Creation
  • Fully Lazy Instantiation
  • Using Generics
  • using Lazy<T> type

You can read each way of Singleton Implementationhere.

10.What is difference between the “throw” and “throw ex” in .NET?

throw re-throws the exception that was caught, and preserves the stack trace. throw ex throws the same exception, but resets the stack trace to that method.Unless you want to reset the stack trace (i.e. to shield public callers from the internal workings of your library), throw is generally the better choice, since you can see where the exception originated.

Throw Syntax:

try { // do some operation that can fail } catch (Exceptionex) { // do some local cleanup throw; } 

Throw ex Syntax:

try { // do some operation that can fail } catch (Exceptionex) { // do some local cleanup throw ex; } } 

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# Interview Questions for Experienced professionals (Part -1)

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址