C# Interview Questions (Beginner Level)
Theory part
Q: What is C# and why do you need it?
It’s type-safe and object-oriented programming language which allows developers to create a large number of different software from little desktop software to large web projects working on .NET platform. Main advantages are:
- It supports several languages for developing
- Using various technologies on the .NET platform, you can develop applications in C # for a wide variety of platforms - Windows, MacOS, Linux, Android, iOS, Tizen
- Great DLL background
- High performance
Besides that, you can use unmanaged code in your project. The last one is JIT compilation which allows you to reach high performance in your software.
Q: What are namespaces and why do we need to use them?
Namespaces are looks like a container for logically combining named entities such as classes, interfaces and others. It’s not an obligatory part of your code but if you will use it you will get well-structured program. You can hook up the namespace and make your code easier to read.
Q: Name basic data types?
Each data type strictly specifies what size, type of variable will be used. The main purpose of strong typing your data is error elimination.
Most common data types in C# are:
- Int
- Long
- Float
- Double
- Bool
- Char
- String
If you don’t need to use negative values you can strictly define with special chars (for example Uint, Ulong). Beside that you can use special type – Object which is connected with main OOP principles.
Q: Tell about main iteration statements?
There are 4 main iteration stamens:
- For statement executes statements until a special Boolean expression evaluates to bool state true. After reaching true condition it will terminate and stop it’ work
- Foreach statement executes statements for each element in an instance of the types that implements two interfaces (IEnumerable, IEnumerable<T>)
- While statement executes statements at least one time and after that checks bool. The loop iterates until checked condition is false.
Q: Tell me about an arrays?
An array represents a set of data of the same type. An array declaration is similar to a variable declaration, except that square brackets are placed after the type Indexes are used to refer to array elements. The index represents the number of the element in the array, with the numbering starting at zero, so the index of the first element will be 0. An array can be Multidimensional. In summary main array concepts are:
- Rank
- Dimension length
- Array length
Q: What do class and OOP mean?
C# is a complete object-oriented language. This means that a C # program can be represented as interconnected interacting objects. A description of an object is a class, and an object represents an instance of that class. You can also draw the following analogy. We all have some idea of a person who has a name, age, some other characteristics. That is, some template - this template can be called a class. The exact implementation of this pattern may differ, for example, some people have one name, others a different name. And a real person (actually an instance of this class) will represent an object of this class. All the functionality of a class is represented by its members - fields (fields are called class variables), properties, methods, events. In addition to ordinary methods, classes also use special methods called constructors. Constructors are called when a new object of this class is created. Constructors perform object initialization.
Q: Overloaded methods, why do we use them?
Sometimes it becomes necessary to create the same method, but with a different set of parameters. And depending on the available parameters, apply a specific version of the method. This feature is also called method overloading.
And in C #, we can create several methods in a class with the same name, but different signatures. What is a signature? The signature consists of the following aspects:
- Method name
- Number of parameters
- Parameter types
- Parameter order
- Parameter modifiers
Method overloading is just that the methods have different signatures, in which only the name of the method is the same. That is, the methods should differ in:
- Number of parameters
- Parameter type
- Order of parameters
- Parameter modifiers
Q: What is exception handling?
Sometimes, when executing a program, errors occur that are difficult to foresee or foresee, and sometimes even impossible. For example, when transferring a file over a network, the network connection may unexpectedly drop. such situations are called exceptions. The C # language provides developers with the ability to handle these situations. This is what the try … catch … finally construct is designed for in C #. When using a try … catch..finally block, all statements in the try block are executed first. If no exceptions have been thrown in this block, then after its execution the finally block starts executing. And then the try..catch..finally construct completes its work. If an exception is thrown in the try block, then normal execution stops and the CLR starts looking for a catch block that can handle the exception. If the desired catch block is found, then it is executed, and after it finishes, the finally block is executed.
Q: Tell me about interfaces?
An interface represents a reference type that can define some functionality - a set of methods and properties without implementation. This functionality is then implemented by classes and structures that use these interfaces. The interface keyword is used to define an interface. Typically, interface names in C # begin with a capital I, for example, IComparable, IEnumerable (the so-called Hungarian notation), but this is not a requirement, but more of a programming style.
What can an interface define? In general, interfaces can define the following entities:
- Methods
- Properties
- Indexers
- Developments
- Static fields and constants (since C # 8.0)
Like classes, interfaces by default have internal access level, that is, such an interface is available only within the current project. But with the public modifier we can make the interface public
Q: Tell me about conditional constructions?
Conditional constructs are one of the basic components of many programming languages that direct a program along one path, depending on certain conditions. The following conditionals are used in C #: if..else and switch..case.
The if / else construction checks the truth of a certain condition and, depending on the results of the test, executes a certain code. A condition is placed after the if keyword. And if this condition is met, then the code that is placed further in the if block after the curly braces is triggered. The previously considered comparison operations are used as conditions. The switch / case construction is similar to the if / else construction, since it allows you to process several conditions at once.
The expression to be compared is shown in parentheses after the switch keyword. The value of this expression is sequentially compared with the values after the case operator. And if a match is found, then a specific case block will be executed.
At the end of each case block, one of the jump statements must be placed: break, goto case, return or throw. Typically, the break statement is used. When applied, other case blocks will not be executed. However, if we want, on the contrary, after the execution of the current case block, another case block is executed, then we can use the goto case statement instead of break