In this article, we will discuss on Interview questions for experienced C# professionals.

How do you generate a random number in C#? How to split string using Regex in C#? How can you get the assembly version in C#? How would you Read settings from app.config or web.config in C#? When you create and compile an application in C#, what all are the files get generated in Debug folder? Can you add extension methods to an existing static class? What is the difference between Object pooling and Connection pooling in C#? What is the use of volatile keyword? What is the use of Partial class in C#? What is async method in C#?

You may also like:

1. How do you generate a random number in C#?

The Random class is used to create random numbers in C#.

//Example: Random r = new Random(); int n = r.Next(); 1 2 3 //Example: Random r = new Random ( ) ; int n = r . Next ( ) ;

2. How to split string using Regex in C#?

The Regular Expressions Split() methods are similar to the String.Split() method, except that Regex.Split() method splits the string at a delimiter determined by a Regular Expression instead of a set of characters.

When using Regular Expressions you should use the following namespace:

using System.Text.RegularExpressions; string str = "test1



test2



test3





test4"; string[] result = Regex.Split(str, "

\\s*"); for (int i = 0; i < result.Length; i++) MessageBox.Show(result[i]); //Output: test1 test2 test3 test4 1 2 3 4 5 6 7 8 9 10 11 using System . Text . RegularExpressions ; string str = "test1



test2



test3





test4" ; string [ ] result = Regex . Split ( str , "

\\s*" ) ; for ( int i = 0 ; i < result . Length ; i ++ ) MessageBox . Show ( result [ i ] ) ; //Output: test1 test2 test3 test4

3. How can you get the assembly version in C#?

You can get the assembly version in C#:

Version version = Assembly.GetEntryAssembly().GetName().Version; 1 Version version = Assembly . GetEntryAssembly ( ) . GetName ( ) . Version ;

4. How would you Read settings from app.config or web.config in C#?

ConfigurationManager.AppSettings["MySetting"] 1 ConfigurationManager . AppSettings [ "MySetting" ]

You will need to add a reference to System.Configuration in your project’s references folder.



5. When you create and compile an application in C#, what all are the files get generated in Debug folder?

When you will create and compile a program in C#, you will see below files in Debug folder.

– exe – the ‘normal’ executable

– vshost.exe – a special version of the executable to aid debuging; see MSDN for details

– pdb – the Program Data Base with debug symbols

– vshost.exe.manifest – a kind of configuration file containing mostly dependencies on libraries



6. Can you add extension methods to an existing static class?

No. Extension methods require an instance of an object. However, you can do something like this:

public static class Extensions { public static T Create<T>(this T @this) where T : class, new() { return Utility<T>.Create(); } } public static class Utility<T> where T : class, new() { static Utility() { Create = Expression.Lambda<Func<T>>(Expression.New(typeof(T).GetConstructor(Type.EmptyTypes))).Compile(); } public static Func<T> Create { get; private set; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static class Extensions { public static T Create < T > ( this T @ this ) where T : class , new ( ) { return Utility < T > . Create ( ) ; } } public static class Utility < T > where T : class , new ( ) { static Utility ( ) { Create = Expression . Lambda < Func < T >> ( Expression . New ( typeof ( T ) . GetConstructor ( Type . EmptyTypes ) ) ) . Compile ( ) ; } public static Func < T > Create { get ; private set ; } }

7. What is the difference between Object pooling and Connection pooling in C#?

The object pool implementation will increase the size of the pool up to the specified maximum. When the maximum is reached,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.

The 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.



8. What is the use of volatile keyword?

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 be applied to fields of these types:

– Reference types.

– Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a “pointer to volatile.”

– Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.

– An enum type with an integral base type.

– Generic type parameters known to be reference types.

– IntPtr and UIntPtr.



9. What is the use of Partial class in C#?



– By Using Partial Classes, multiple developer can work on the same class easily.

– Partial classes are mainly used by code generator to keep different concerns separate

– you can also define Partial methods as well where a developer can simply define the method and the other developer can implement that.



10. What is an async method in C#?

An async method is a method that returns to the calling method before completing all its work, and then completes its work while the calling method continues its execution.

An async method has the following characteristics:

– An async method must have the async keyword in its method header, and it must be before the return type.

– This modifier doesn’t do anything more than signal that the method contains one or more await expressions.

– It contains one or more await expressions. These expressions represent tasks that can be done asynchronously.

– It must have one of the following three return types.

− void :If the calling method just wants the async method to execute, but doesn’t need any further interaction with it

− Task : If the calling method doesn’t need a return value from the async method, but needs to be able to check on the async method’s state

− Task :If the calling method is to receive a value of type T back from the call, the return type of the async method must be Task

– An async method can have any number of formal parameters of any types but it cannot be out or ref parameters.

– The name of an async method should end with the suffix Async.

– Otherthan Methods, lambda expressions and anonymous methods can also act as async objects.

You can read more on async and await here.



You may also like:

Thanks for visiting !!

© 2016, Csharp Star. All rights reserved.