C# Interview Questions (part-1)
Which class is super class of all classes in .NET Classes?
Object class ( System.Object ) is the base class of .NET Classes
What is the entry point for a C# console application?
The Main method is the execution entry point of C# console application.
Why Main() method is static?
You need an entry point into your program. A static method can be called without instantiating an object. Therefore main() needs to be static in order to allow it to be the entry to your program.
What is string args in Main method? What is there use?
The parameter of the Main method is a String array that represents the command-line arguments . The string args may contain any number of Command line arguments which we want to pass to Main() method.
Is .Net Pass by Value or Pass by Reference?
By default in .Net, you don't pass objects by reference . You pass references to objects by value. However, you get the illusion it's pass by reference, because when you pass a reference type you get a copy of the reference (the reference was passed by value).
What is nullable type in c#?
Nullable type is new concept introduced in C#2.0 which allow user to assign null value to primitive data types of C# language. It is important to note that Nullable type is Structure type.
What is Upcasting ?
Upcasting is an operation that creates a base class reference from a subclass reference. (subclass -> superclass) (i.e. Manager -> Employee)
What is Downcasting?
Downcasting is an operation that creates a subclass reference from a base class reference. (superclass -> subclass) (i.e. Employee -> Manager)
What is managed/unmanaged code?
- Managed code is not directly compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a secure framework which handles things like memory and threads for you.
- Unmanaged code is compiled directly to machine code and therefore executed by the OS directly. So, it has the ability to do damaging/powerful things Managed code does not.
C# is managed code because Common language runtime can compile C# code to Intermediate language.
Can I override a static methods in C#?
No. You can't override a static method. A static method can't be virtual, since it's not related to an instance of the class.
Can we call a non-static method from inside a static method?
Yes. You have to create an instance of that class within the static method and then call it, but you ensure there is only one instance.
Explain namespaces in C#?
Namespaces are containers for the classes. Namespaces are using for grouping the related classes in C#. "Using" keyword can be used for using the namespace in other namespace.
What is the % operator?
It is the modulo (or modulus) operator. The modulus operator (%) computes the remainder after dividing its first operand by its second.
What is Jagged Arrays?
The array which has elements of type array is called jagged array . The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.
Difference between break and continue statement?
- Break statement: If a particular condition is satisfied then break statement exits you out from a particular loop or switch case etc.
- Continue statement: When the continue statement is encountered in a code, all the actions up till this statement are executed again without execution of any statement after the continue statement.
Difference between an if statement and a switch statement?
The if statement is used to select among two alternatives only. It uses a boolean expression to decide which alternative should be executed. While the switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.
Difference | prefix and postfix forms of the ++ operator?
- Prefix:increments the current value and then passes it to the function.
Above code return 11 because the value of i is incremented, and the value of the expression is the new value of i.
- Postfix: passes the current value of i to the function and then increments it.
Above code return 20 because the value of i is incremented, but the value of the expression is the original value of i
Can a private virtual method be overridden?
No, because they are not accessible outside the class.
Can you store multiple data types in System.Array?
No , because Array is type safe.
If you want to store different types, use:
What's a multicast delegate in C#?
Multicast delegate is an extension of normal delegate . It helps you to point more than one method at a single moment of time.
Can a Class extend more than one Class?
It is not possible in C#, use interfaces instead.
Can you define private and protected modifiers for variables in interfaces?
Interface is like a blueprint of any class, where you declare your members. Any class that implement that interface is responsible for its definition. Having private or protected members in an interface doesn't make sense conceptually. Only public members matter to the code consuming the interface. The public access specifier indicates that the interface can be used by any class in any package.
When does the compiler supply a default constructor for a class?
In the CLI specification, a constructor is mandatory for non-static classes, so at least a default constructor will be generated by the compiler if you don't specify another constructor. So the default constructor will be supplied by the C# Compiler for you.
How destructors are defined in C#?
C# destructors are special methods that contains clean up code for the object. You cannot call them explicitly in your code as they are called implicitly by GC.
In C# they have same name as the class name preceded by the ~ sign.
What is a structure in C#?
In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.
What's the difference between the Debug and Trace class?
The Debug and Trace classes have very similar methods. The primary difference is that calls to the Debug classes are typically only included in Debug build and Trace are included in all builds (Debug and Release).
What is the difference between == and quals() in C#?
The "==" compares object references and .Equals method compares object content
Can you create abstract classes without any abstract methods?
Yes, you can. There is no requirement that abstract classes must have abstract methods. An Abstract class means the definition of the class is not complete and hence cannot be instantiated.
Can you have static methods in interface?
You can't define static members on an interface in C#. An interface is a contract, not an implementation.
Can you use "this" inside a static method in C#?
No. Because this points to an instance of the class, in the static method you don't have an instance.
Can you inherit multiple interfaces?