C Sharp Interview Questions
Interview questions for C# developers
- Is it possible to inline assembly or IL in C# code? -
No. - Is it possible to have different access modifiers on the get/set methods of a property? -
No. The access modifier on a property applies to both its get and set accessors. What you need to do if you want them to be different is make the property read-only (by only providing a get accessor) and create a private/internal set method that is separate from the property. - Is it possible to have a static indexer in C#? -
No. Static indexers are not allowed in C#.
C# .NET interview questions
1. Are private class-level variables inherited? -
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
2. Why does DllImport not work for me? -
All methods marked with the DllImport attribute must be marked as public static extern.
3. Why does my Windows application pop up a console window every time I run it? -
Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If you’re using the command line, compile with /target:winexe, not /target:exe.
C# .NET interview questions II
11. What is the difference between a struct and a class in C#? -
From language spec: The list of similarities between classes and structs is as follows. Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.
C# developer interview questions
1) The C# keyword ‘int’ maps to which .NET type?
-
System.Int16
-
System.Int32
-
System.Int64
-
System.Int128
2) Which of these string definitions will prevent escaping on backslashes in C#?
-
string s = #”n Test string”;
-
string s = “’n Test string”;
-
string s = @”n Test string”;
-
string s = “n Test string”;
3) Which of these statements correctly declares a two-dimensional array in C#?
-
int[,] myArray;
-
int[][] myArray;
-
int[2] myArray;
-
System.Array[2] myArray;
Advanced C# interview questions
- What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.
- Can you store multiple data types in System.Array? No.
- What’s the difference between the System.Array.CopyTo() and System.Array.Clone()? The first one performs a deep copy of the array, the second one is shallow.
- How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.