50 C# Technical Interview Questions and Answers Part 4

Опубликовано: 06 Октябрь 2024
на канале: tutorialsEU
5,071
176

This is part 4 of the Series C# Technical Interview - 50 Questions and Answers. Get the complete c# Masterclass for 90% off https://bit.ly/32iZG5G
Also, check out the article for this video:
https://tutorials.eu/50-interview-que...

Part 1:    • C# Interview 50 Questions and Answers...  
Part 2:    • C# Interview 50 Questions and Answers...  
Part 3:    • C# Technical Job Interview 50 Questio...  

Stay tuned and subscribe to tutorialsEU: https://goo.gl/rBFh3x

31. What are the different types of delegates?
There are three types of delegates.
Single Delegate: Single Delegate can invoke a single method.
Multicast Delegate: Multicast delegates can invoke multiple methods. The delegate method can do multicasting. We can add a method in the delegate instance using + operator, and we can remove a method using - operator. All the methods are invoked in sequence as they are assigned.
Generic Delegate:  .Net Framework 3.5 Introduces Generic delegates. There is no need to create an instance in a generic delegate.

32. What is an array? Explain Single and multi-dimensional arrays.
The array stores the value of the same type. It is a collection of variable stores into a memory location.
For example
int[] marks = new int[3] { 25, 34, 89 };
A single-dimensional array is a linear array. Single-dimensional array stores variables in a single row. The above example is a single-dimensional Array.
Arrays can have more than one dimension. Multi-dimensional arrays are also called rectangular Arrays.
For example
int[,] numbers = new int[3, 2] { { 1, 2 }, { 2, 3 }, { 3, 4 } };

33. What is the difference between the System.Array.CopyTo() and System.Array.Clone() ?
Using the Clone() method, we can create a new array object containing all the elements of the original array and using the CopyTo() method. All the items of existing array copies into another existing array. Both ways create a shallow copy.
int[] marks = new int[3] { 25, 34, 89 };
int[] marksCopy;
int[] marksClone = (int[])marks.Clone();
marks.CopyTo(marks,0);
34. What is the difference between array and arraylist?
When we want to store the items of the same type, then the array is used. The array has a fixed size but not in the case of an arraylist. We can store any type of data type in an array list.

35. What is a jagged array in C#?
A jagged array is like a nested array; each element of a jagged array is an array in itself. The item of a jagged array can be of different dimensions and sizes.
A jagged array is a special type of array introduced in c#. A Jagged array is an array of an array in which the length of each array index can differ.

36. What is the difference between struct and class?
Class and struct are both user-defined but have a significant difference.
Struct
A Struct inherits from System. Value type and so it is a value type.
It is preferred to use struct when there is a small amount of data. 
A structure cannot be abstract.
There is no need to create an object with a new keyword.
Struct does not have permission to create any default constructor.

Class
The class is a reference type in c#, and it inherits from the System. Object Type.
When there is a large amount of data, then in that scenario, classes are used.
We can inherit one class from another class.
A class can be an abstract type.
37. What is the difference between throw and throw ex?

"Throw" statement holds the original error stack of the previous function or function hierarchy. In contrast, "Throw ex" has the stack trace from the throw point.  So it is always advised to use "Throw" because it provides exact error information related to the function and gives you actual trace data of error source.

38. Explain the difference between finally and finalize block?

Finally, it is a code block part of execution handling this code block executes irrespective of whether the exception occurs or not.

While the finalize method is called just before garbage collection. The compiler calls this method automatically when not called explicitly in code.

39. Explain var and dynamic.

We can declare the var type of a variable without specifying the .net data types explicitly. The compiler automatically detects the type of a variable at the compile-time based on the value assigned to it. We cannot declare a var type variable without assigning a value to it. The value of the var type variable cannot be changed later in the code.

Dynamic is the opposite of var. We can change the value of a variable of type dynamic in the code later. It also decides the type of the variable based on the value assigned to it. 

40. What are Anonymous types in C#?

Anonymous types allow us to create new types without defining them. 

When there is a need to define read-only properties in a single object without defining each type. In that case, we use anonymous types. Here, a compiler generates type and is accessible only for the current block of code.