C# (C-Sharp) is a widely used programming language in a variety of enterprise-level applications developments. So, if you are a programmer, it’s vital to prepare for the highly technical C# interview questions you may be faced with when you are called for a job interview in this field.
An object-oriented (OO) programming language, C# was developed by Anders Hejlsberg at Microsoft in 2000. Almost two decades on, Hejlsberg has moved on (to the development of Microsoft’s TypeScript), and C# has become the world’s most powerful and famous programming language for development of applications.
The language was developed for use within Microsoft’s .NET development framework, a large library of code classes (handling such things as network communications, and data connectivity and access) and the Common Language Runtime software environment (simple to the open-source Mono framework and environment for Linux and iOS), and so proficiency in C# is regarded as a highly sought-after skill in the development of Windows desktop applications.
Because of the degree of collaboration and sharing between developers and programmers, there is a huge volume of C# interview questions and answers available online, and below we have compiled a mini-list of 7 as an indication of what you can expect from C# interviewers.
Greg Unger’s Rockin the C# Interview: 2021 Edition is a great place to start, although you should also look up titles such as the Vibrant Publishers series C# Interview Questions You’ll Most Likely Be Asked as well as Terry Sanchez-Clark’s comprehensive ]C# Interview Questions, Answers, and Explanations.
Typical C# interview questions asked of applications development job candidates
Here are 7 typical C# interview questions to get you into the right frame of mind to face a job interview. AGENT cannot guarantee that these questions will come up, of course, or that the answers provided here will be what the interviewer is looking for in your particular instance, but careful study of these, and testing of the answers supplied, with additional research of any of the books mentioned above, will put you in a strong position to handle any C# Interview questions that could be asked of you on the day
1. What is C#? or How would you describe C#?
You couldn’t get C# interview questions as easy as this one, right? Wrong. Interviewers are looking for evidence of a rounded package of comprehensive knowledge and skill, so be thorough and structured in your response. Here, according to the team at C-SharpCorner.com, is a good answer to this question:
C# is the best language for writing Microsoft .NET applications. C# provides the rapid application development found in Visual Basic with the power of C++. Its syntax is similar to C++ syntax and meets 100% of the requirements of OOPs like the following:
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
The writers suggest having a good general knowledge of the C# language, as well as the additional features in the most recent editions, C# 6.0 and 7.0. Always stay up to date with the latest developments!
2. What are attributes in C#?
Don’t be fooled by the apparent simplicity of C# interview questions like these. Dashing off a perfunctory answer here will not do. You have to show that you know what you’re talking about, and be able to provide examples. Here is what Rashed, a contributor to Cybarlab.com suggests as an answer.
Start off by keeping it simple: attributes are declarative tags, used to convey information about the behaviours of various elements (classes, methods, assemblies, structures, enumerators, etc). Attributes are accessed at Compile Time or Run Time. Attributes are declared with square brackets [], placed above the elements
You could also pick up bonus points by explaining why attributes are used (for adding metadata); describing pre-defined attributes (AttributeUsage; Conditional; and Obsolete) and custom-built attributes (such as those that can be created on the Microsoft .Net Framework for storing declarative information and retrieved at Run Time).
3. Questions on Output in C#
The Toptal.com website provides the following as an example of a technical question in output that could be asked in an interview:
What is the output of the short program below? Explain your answer.
class Program { static String location; static DateTime time; static void Main() { Console.WriteLine(location == null ? "location is null" : location); Console.WriteLine(time == null ? "time is null" : time.ToString()); } }
The answer is that, although both variables are uninitialized, String is a reference type and DateTime is a value type. As a value type, an unitialized DateTime variable is set to a default value of midnight of 1/1/1 (the year 1 A.D.), not null.
4. What is the difference between Public, Static and Void?
According to CareerGuru99.com, the answer is as follows:
- Public declared variables or methods are accessible anywhere in the application.
- Static declared variables or methods are globally accessible without creating an instance of the class. Static member are by default not globally accessible it depends upon the type of access modified used. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created.
- Void is a type modifier that states that the method or variable does not return any value.
5. What is Partial Class and why Microsoft has introduced it?
Iminfo.in, a useful online resource with guidance for questions that would arise during job interviews for a variety of programming languages and roles. In relation to C# interview questions, the Iminfo writers advise that attention to detail is important, and that the interviewers are always looking for a little bit extra. Here is their answer to this question:
Partial Class enables the splitting of the definition of a class over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
Microsoft introduced it because, when working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
However, one writer suggests that the interviewer was expecting more information or a different explanation as to why Microsoft introduced Partial Class. The writer suggests exploring the notion that Microsoft had a requirement to produce automatically generated source code. Virtual Studio uses that approach when it creates Windows Forms, Web service wrapper code, and so on. So in that context, code can be created that uses these classes without having to modify the file created by Visual Studio.
The writer suggests that exploring these kinds of possibilities may enhance an interviewer’s impression of the candidate.
6. What makes your code really Object-Oriented? or What are the fundamental principles of OO programming?
According to the writers on the CodeMentor.io website, C# a programming language that was developed with Object Orientation (OO) in mind. However, most developers don’t fully exploit OO. When writing code, it resembles actual procedural and structured programming constructs wrapped up into classes, rather than fundamental OO.
In order to understand C# interview questions such as this, it’s vital to understand the benefits of OO, and understand its foundation (hence the likely variant of the question provided above).
Developers might be tempted to answer that their code is OO because it comprises Encapsulation, Polymorphism, Abstraction, and Inheritance. Although this is true, it doesn’t really explain the fundamental core of OO and its benefits.
Obviously, there are principles like Encapsulation, Polymorphism, Abstraction, and Inheritance, but these are the consequence and not the generating force behind the OO paradigm in C#, but the two most fundamental core concepts of OO in C# are this pointer and Dynamic Dispatch.
7. Code-writing assignments
Provided by the code experts at Toptal.com, here is a typical example of a code-writing assignment that might arise during a round of technical C# interview questions
Given an instance circle of the following class, …
class Program { static String location; static DateTime time; static void Main() { Console.WriteLine(location == null ? "location is null" : location); Console.WriteLine(time == null ? "time is null" : time.ToString()); } }
… write code to calculate the circumference of the circle, without modifying the Circle class itself.
The preferred answer to this question, according to Toptal.com would be of the form:
circle.Calculate(r => 2 * Math.PI * r);
Since we don’t have access to the private radius field of the object, we tell the object itself to calculate the circumference, by passing it the calculation function inline. Many C# programmers shy away from (or don’t understand) function-valued parameters. While in this case the example is a little contrived, the purpose is to see if the applicant understands how to formulate a call to Calculate which matches the method’s definition.
An alternative, albeit less elegant, solution is to retrieve the radius value itself from the object and then perform the calculation with the result:
var radius = circle.Calculate(r => r); var circumference = 2 * Math.PI * radius;
Either way works. The main thing interviewers are looking for is the candidate’s familiarity with, and understanding of how to invoke, the Calculate method.