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.

static void main(string args[]) { }

Why Main() method is static?

In order to establish an entry point for your program, it is essential to designate a specific method that serves this purpose. By declaring a method as static, it can be invoked without the need to create an instance of the associated object. Consequently, the main() method, which serves as the entry point for your program, needs to be declared as static. This allows it to be accessed and executed directly, ensuring a seamless initiation of program execution. The static nature of the main() method enables it to act as the starting point for your program's execution flow, facilitating the systematic execution of subsequent instructions and operations.

What is string[] args in Main method? What is there use?

The Main method, upon invocation, accepts a parameter of type String array, which serves as a container for the command-line arguments passed to the program. This string[] args parameter can accommodate any number of command line arguments that are intended to be received and processed within the Main() method. By using this parameter, developers gain the ability to access and utilize the command line arguments provided when executing the program. This flexibility allows for the integration of external inputs and facilitates the customization and dynamic behavior of the program based on the specific arguments passed during runtime.

Is .Net Pass by Value or Pass by Reference?

In the default behavior of .NET, objects are not passed by reference; instead, references to objects are passed by value. This means that when you pass a reference type as an argument, a copy of the reference itself is passed, not the actual object. As a result, there is an illusion of pass by reference, as modifications made to the passed reference affect the original object. However, it's crucial to understand that the reference itself is being passed by value, which means that reassigning the reference within the method will not affect the original reference outside the method.

What is nullable type in c#?

The introduction of nullable types in C# 2.0 brought forth a novel concept, enabling users to assign null values to primitive data types within the C# language. It is crucial to recognize that nullable types are a specific category of structure types, imbued with the ability to represent the absence of a value through the assignment of null. This distinction sets nullable types apart from their non-nullable counterparts, as they provide a means to accommodate scenarios where the absence of a value is a valid and meaningful state.

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?

  1. 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.
  2. 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 serve as organizational containers for classes in C#. They provide a means of grouping related classes together, facilitating better code organization and modularization. The "using" keyword is employed to conveniently import and access namespaces within other namespaces. By utilizing the "using" keyword, developers can establish a seamless and concise way to reference and utilize classes from other namespaces, promoting code readability and maintainability.

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.

5 % 2 = 1

What is Jagged Arrays?

A jagged array, also referred to as an array of arrays, is a specialized array in which the elements themselves are arrays. What distinguishes a jagged array is that each element can have a different dimension and size compared to the others. This characteristic allows for a flexible and versatile data structure that can accommodate varying array lengths within its structure. By using jagged arrays, developers gain the ability to handle complex and diverse data scenarios, where the individual arrays can differ in shape and size, providing a powerful tool for organizing and manipulating data in a structured manner.

Difference between break and continue statement?

  1. Break statement: If a particular condition is satisfied then break statement exits you out from a particular loop or switch case etc.
  2. 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 serves as a control structure employed to make a decision between two distinct alternatives. It relies on a boolean expression, typically involving a condition, to determine which alternative should be executed based on the evaluation of the expression. On the other hand, the "switch" statement is specifically designed for scenarios involving multiple alternatives. It employs an integer expression, such as a variable or constant, to ascertain which alternative should be executed by evaluating the expression against a series of specified cases. By utilizing the "switch" statement, developers gain a concise and efficient means of selecting the appropriate alternative among multiple possibilities based on the value of the expression being evaluated.

Difference | prefix and postfix forms of the ++ operator?

  1. Prefix:increments the current value and then passes it to the function.
i = 10; Console.WriteLine(++i);

Above code return 11 because the value of i is incremented, and the value of the expression is the new value of i.

  1. Postfix: passes the current value of i to the function and then increments it.
i = 20; Console.WriteLine(i++);

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:

System.Collections.ArrayList or object[]

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?

An interface can be perceived as a blueprint or template for a class, defining the members that any class implementing that interface must adhere to. The responsibility of defining and implementing these members lies with the class that implements the interface. Conceptually, the inclusion of private or protected members within an interface lacks significance since their accessibility is restricted to the defining class itself. In an interface, only public members hold relevance as they define the contract that other code consuming the interface can rely upon. By designating the public access specifier, the interface becomes accessible to any class within any package, allowing for wider utilization and interaction with the interface's defined members.

When does the compiler supply a default constructor for a class?

As per the CLI specification, non-static classes are required to have a constructor. In cases where a constructor is not explicitly provided by the developer, the C# compiler automatically generates a default constructor. This default constructor is supplied to ensure that an instance of the class can be properly initialized. Therefore, if no constructor is defined, the C# compiler steps in and creates a default constructor on your behalf, fulfilling the mandatory requirement set by the CLI specification.

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 the C# programming language, a structure is classified as a value type data type. It serves as a valuable tool for consolidating related data of diverse data types into a single variable. To create a structure, the struct keyword is utilized, signaling the compiler to define a custom data type with its associated members and behaviors. Structures offer several benefits, such as efficient memory allocation and improved performance due to their value type nature. By employing structures, developers can organize and encapsulate data in a cohesive manner, enhancing code readability and maintainability.

What's the difference between the Debug and Trace class?

The Debug and Trace classes in C# exhibit a high degree of similarity in terms of their available methods. However, a key distinction lies in their usage and inclusion within different build configurations. Debug calls, associated with the Debug class, are primarily intended for utilization during Debug builds. These calls serve as invaluable aids during the development and testing phase, enabling developers to diagnose and rectify issues effectively. On the other hand, Trace calls, associated with the Trace class, are included in all build types, encompassing both Debug and Release builds. This broader inclusion ensures that tracing information and diagnostics are available in all build configurations, facilitating comprehensive monitoring and analysis.

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. Abstract classes in C# do not necessarily need to include abstract methods. The presence of abstract methods is not a mandatory requirement for an abstract class. Instead, an abstract class signifies that the class itself is incomplete and cannot be instantiated. This incomplete nature of an abstract class indicates that it is intended to serve as a base or blueprint for derived classes, providing a framework for common functionality and characteristics. The absence of a complete implementation within an abstract class ensures that it cannot be directly instantiated, as it lacks the necessary implementation details.

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?

Yes..you can