C# - .NET interview questions and answers [Part 1]
In the following guide, we will cover some of the most important aspects to pass a .NET/C# job technical interview.
What is .NET Core?
The .NET Core platform is a new .NET stack that is optimized for open source development and agile delivery on NuGet.
What is the difference between .NET Core and Mono?
Mono is the third party implementation of .Net Framework for Linux/Android/iOs
.Net Core is Microsoft's own implementation for the same.
What are some characteristics of .NET Core?
Flexible deployment: Can be included in your app or installed side-by-side user- or machine-wide.
Cross-platform: Runs on Windows, macOS, and Linux; can be ported to other OSes. The supported Operating Systems (OS), CPUs, and application scenarios will grow over time, provided by Microsoft, other companies, and individuals.
Command-line tools: All product scenarios can be exercised at the command-line.
Compatible: .NET Core is compatible with .NET Framework, Xamarin, and Mono, via the .NET Standard Library.
Open source: The .NET Core platform is open source, using MIT and Apache 2 licenses. Documentation is licensed under CC-BY. .NET Core is a .NET Foundation project.
Supported by Microsoft: .NET Core is supported by Microsoft, per .NET Core Support.
What's the difference between SDK and Runtime in .NET Core?
The SDK is all of the stuff that is needed/makes developing a .NET Core application easier, such as the CLI and a compiler.
The runtime is the "virtual machine" that hosts/runs the application and abstracts all the interaction with the base operating system.
What is CTS?
The Common Type System (CTS) standardizes the data types of all programming languages using .NET under the umbrella of .NET to a common data type for easy and smooth communication among these .NET languages.
CTS is designed as a singly rooted object hierarchy with System.Object as the base type from which all other types are derived. CTS supports two different kinds of types:
Value Types: Contain the values that need to be stored directly on the stack or allocated inline in a structure. They can be built-in (standard primitive types), user-defined (defined in source code) or enumerations (sets of enumerated values that are represented by labels but stored as a numeric type).
Reference Types: Store a reference to the value‘s memory address and are allocated on the heap. Reference types can be any of the pointer types, interface types, or self-describing types (arrays and class types such as user-defined classes, boxed value types, and delegates).
Can ASP.NET Core work with the .NET framework?
Yes. This might surprise many, but ASP.NET Core works with .NET framework and this is officially supported by Microsoft.
ASP.NET Core works with:
.NET Core framework
.NET framework
What is Kestrel?
Kestrel is a cross-platform web server built for ASP.NET Core based on libuv – a cross-platform asynchronous I/O library.
It is a default web server pick since it is used in all ASP.NET Core templates.
It is really fast.
It is secure and good enough to use it without a reverse proxy server. However, it is still recommended that you use IIS, Nginx, or Apache or something else.
What is the difference between .NET Core and .NET Framework?
.NET as a whole now has 2 flavors:
.NET Framework
.NET Core
.NET Core and the .NET Framework have (for the most part) a subset-superset relationship. .NET Core is named “Core” since it contains the core features from the .NET Framework, for both the runtime and framework libraries. For example, .NET Core and the .NET Framework share the GC, the JIT, and types such as String and List.
.NET Core was created so that .NET could be open-source, cross-platform, and be used in more resource-constrained environments.
Explain what is included in .NET Core?
A .NET runtime, which provides a type system, assembly loading, a garbage collector, native interop, and other basic services.
A set of framework libraries, which provide primitive data types, app composition types and fundamental utilities.
A set of SDK tools and language compilers that enable the base developer experience, available in the .NET Core SDK.
The 'dotnet' app host, which is used to launch .NET Core apps. It selects the runtime and hosts the runtime, provides an assembly loading policy, and launches the app. The same host is also used to launch SDK tools in much the same way.
What is CoreCLR?
CoreCLR is the .NET execution engine in .NET Core, performing functions such as garbage collection and compilation to machine code.
Explain the difference between Task
and Thread
in .NET (Take a better look for it)
Task
and Thread
in .NET (Take a better look for it)The thread represents an actual OS-level thread, with its own stack and kernel resources. The thread allows the highest degree of control; you can Abort() or Suspend() or Resume() a thread, you can observe its state, and you can set thread-level properties like the stack size, apartment state, or culture. ThreadPool is a wrapper around a pool of threads maintained by the CLR.
The Task class from the Task Parallel Library offers the best of both worlds. Like the ThreadPool, a task does not create its own OS thread. Instead, tasks are executed by a TaskScheduler; the default scheduler simply runs on the ThreadPool. Unlike the ThreadPool, Task also allows you to find out when it finishes, and (via the generic Task) to return a result.
What is a JIT compiler? (Just-in-time)
Before a computer can execute the source code, special programs called compilers must rewrite it into machine instructions, also known as object code. This process (commonly referred to simply as “compilation”) can be done explicitly or implicitly.
The implicit compilation is a two-step process:
The first step is converting the source code to intermediate language (IL) by a language-specific compiler.
The second step is converting the IL to machine instructions. The main difference with the explicit compilers is that only executed fragments of IL code are compiled into machine instructions, at runtime. The .NET framework calls this compiler the JIT (Just-In-Time) compiler.
What are the benefits of explicit compilation?
Ahead of time (AOT) delivers faster start-up time, especially in large applications where much code executes on startup. But it requires more disk space and more memory/virtual address space to keep both the IL and precompiled images. In this case, the JIT Compiler has to do a lot of disk I/O actions, which are quite expensive.
What is Managed or Unmanaged Code?
Managed Code
“Managed code is the code that is developed using the .NET framework and its supported programming languages such as C# or VB.NET. Managed code is directly executed by the Common Language Runtime (CLR or Runtime) and its lifecycle including object creation, memory allocation, and object disposal is managed by the Runtime. Any language that is written in .NET Framework is managed code".
Unmanaged Code
The code that is developed outside of the .NET framework is known as unmanaged code.
“Applications that do not run under the control of the CLR are said to be unmanaged. Languages such as C or C++ or Visual Basic are unmanaged.
The object creation, execution, and disposal of unmanaged code is directly managed by the programmers. If programmers write bad code, it may lead to memory leaks and unwanted resource allocations.”
The .NET Framework provides a mechanism for unmanaged code to be used in managed code and vice versa. The process is done with the help of wrapper classes.
What is Boxing and Unboxing in C#? (Find out some examples for real life)
Boxing and Unboxing both are used for type conversions. The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Here is an example of boxing in C#.
The process of converting from a reference type to a value type is called unboxing. Here is an example of unboxing in C#.
What is the difference between a struct and a class in C#?
Class and struct are both user-defined data types, but have some major differences:
Struct
Struct is usually used for smaller amounts of data.
Struct can’t be inherited from other types.
A structure can't be abstract.
No need to create an object with a new keyword.
Do not have permission to create any default constructor.
Class
Classes are usually used for large amounts of data.
Classes can be inherited from other classes.
A class can be an abstract type.
We can create a default constructor.
What is the difference between Interface and Abstract Class in C#?
A class can implement any number of interfaces but a subclass can at most use only one abstract class.
An abstract class can have non-abstract methods (concrete methods) while in case of interface, all the methods have to be abstract.
An abstract class can declare or use any variables while an interface is not allowed to do so.
In an abstract class, all data members or functions are private by default while in an interface all are public, we can’t change them manually.
In an abstract class, we need to use abstract keywords to declare abstract methods, while in an interface we don’t need to use that.
An abstract class can’t be used for multiple inheritances while the interface can be used as multiple inheritances.
An abstract class uses a constructor while in an interface we don’t have any type of constructor.
An INTERFACE in C# is a type definition similar to a class, except that it purely represents a contract between an object and its user. It can neither be directly instantiated as an object, nor can data members be defined. So, an interface is nothing but a collection of methods and property declarations.
A class or a struct can implement one or more interfaces implicitly or explicitly. Use public modifier when implementing interface implicitly, whereas don't use it in case of explicit implementation.
What is enum in C#?
An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type that is user-defined. An enum type can be an integer (float, int, byte, double, etc.). But if you use it beside int it has to be cast.
Enums are enumerated data types in c#.
Enums are not for the end-user, they are meant for developers.
Enums are strongly typed constants. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members is the same.
Enumerations (enums) make your code much more readable and understandable.
Enum values are fixed. Enum can be displayed as a string and processed as an integer.
The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
Every enum type automatically derives from System. Enum and thus we can use System.Enum methods on enums.
Enums are value types and are created on the stack and not on the heap.
An enum
is a special "class" that represents a group of constants (unchangeable/read-only variables).
Last updated