Microsoft C# is one of the most popular programming languages and C# is widely used for asp.net application development, thus gaining popularity among web application developer. C# is an object oriented programming language, so rather than just memorising the question, try to understand the concept behind each questions answers.
.Net C# job interview questions and answers for fresher and experienced developersHere are some commonly asked .Net C# Interview Questions
Function Overloading is a type Polymorphism, means one function with different parameters, look at example below
public void HelloWorld() { Console.writeline("Hello How are you?"); } public void HelloWorld(string name) { Console.writeline(string.format("Hello {0}, How are you?", name)); }
Overriding is used when we implement any function that has been declared as either abstract or virtual, then in child class at the time of implementation we need to use override the base.
Generics are the new feature in the c# 2.0. They introduce the concept of Type parameter. This makes it possible to classes and methods that differs the specification of one or more types until the class or method is declared and used by the code, which will be using it.
We can declare a generic class like thispublic class GenericClass<T> { void Add(T input) { } }
We create an instance of the generic class like this
GenericClass<string> list2 = new GenericClass<string>();
Read more about generic
The System.Collection.ArrayList can be used with any objectn, but no type checking is done when objects are passed to methods. You have to manually cast objects back to our type when retrieving; which makes the code harder.
Class and struct both are the user defined data type, but they have some difference
Struct | Class |
1. The struct is value type in C# and it inherits from System.Value Type. | 1. The class is reference type in C# and it inherits from the System.Object Type |
2. Struct is usually used for smaller amounts of data | 2. Classes are usually used for large amounts of data |
3. Structure can't be abstract | 3. Class can be abstract type |
4. Struct can't be inherited to other type | 4. Classes can be inherited to other class. |
5. Can not create any default constructor. | 5. Can create a default constructor |
Stack | Heap |
1. Stack is used for static memory allocation | 1. Heap for dynamic memory allocation |
2. Stack memory allocated at compiled time, access to this memory is very fast | 2. Heap memory are allocated at run time and accessing this memory is a bit slower |
3. Stack is value type | 3. Heap reference type |
// Stack example int i=10; |
// Heap example Student stu=new Student(); |
4. Struct, Enum, byte, decimal, double, float, long | 4. string , class, interface, object |
Boxing is assigning a value type to reference type variable, unboxing is opposite.
boxing example
int i=100; object o =i; |
unboxing example
object o=100; int i =(int)o; //unboxing |
Output parameters are similar to reference parameters, except that they transfer data out of the method rather than within method.
Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.
C# is managed code because Common language runtime can compile C# code to Intermediate language.
Declare the class as public and make the method sealed to prevent it from being overridden.
Protected Internal variables/methods are accessible within the same assembly and also from the classes that are derived from this parent class.
Learn more about access modifiers and keywords in C#
class Program { static String location; static DateTime time; static void Main(string[] args) { Console.WriteLine(location == null ? "location is null" : location); Console.WriteLine(time == null ? "time is null" : time.ToString()); } }
The first statement will print, “location is null”.
And the second statement will print default time,
the time variable is not null unless it's specified as DateTime?
Method overloading is creating multiple methods with the same name with unique signatures in the same class. When we compile, the compiler uses overload resolution to determine any specific method to be invoked.
Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.
enumeration cannot inherit or cannot pass inheritance, in C# enumerations are value data type.
Copy()
method, we copy the whole object with data, in clone Clone()
method, we copy only structure without data.
Reflection is functionalities by which we can get metadata information about loaded assemblies.
All class for using Reflection under System.Reflection
System.ReflectionA thread pool is a collection of threads that can be used to perform several tasks in the background.
Once a thread in the pool completes its task, it is returned to a queue of waiting threads, where it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task.
Learn more about Threading
int[] InArray= new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Here are two methods to get the sum of even and odd number of an array.
public static long SumAllEvenNumbers(int[] intArray) { return intArray.Where(i => i % 2 == 0) .Sum(i => (long)i); } public static long SumAllOddNumbers(int[] intArray) { return intArray.Where(i => i % 2 != 0) .Sum(i => (long)i); }
class Program { delegate void Printer(); static void Main(string[] args) { List<Printer> printers = new List<Printer>(); for (int i = 0; i < 10; i++) { printers.Add(delegate { Console.WriteLine(i); }); } foreach (var printer in printers) { printer(); } } }
Public is an access modifier, public keyword indicates that the declared variables or methods are accessible anywhere in the application.
Static indicates, without creating the new instance or object of a class we can access that member directly.
Void indicates that the type of return is nothing, so that's called void.
Dispose()
is called when we want for an object to release any unmanaged resources with them, we can use IDisposable interface to implement dispose method, On the other hand,
Finalize method Finalize()
is used for the same purpose, but it doesn't assure the garbage collection of an object, we also can not call Finalize method, it gets executed internally.
using System; using System.Configuration; public static BusinessSettings GetBusinessSettings(string sectionPath) { return ConfigurationManager.GetSection(sectionPath) as BusinessSettings; }
You may look at Read appSettings json in .Net Core Console Application.
Tuples means more than one data type, Tuple was introduced in C# 7 version.
We can define Tuples variable like example below, we also can return Tuples data type from any function in c# program.
(string Alpha, string Beta) namedLetters = ("a", "b"); Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
There are many new features was introduced in C# 7, like Tuples, out variable, Pattern matching, async Main, default literal expressions etc.
learn more about c# New Features and latest version.