Swipe left or tap Next ▶ to turn the page.
Select any text to highlight it or add a note. Tap a diagram to enlarge it.
C# Basics: .NET, Types, Value vs Reference, Strings, Control Flow, Arrays and Nullable
- ✔ Explain CLR, IL, JIT, CTS and the .NET runtime clearly in an interview
- ✔ Pick the right C# type and predict conversions, overflow and operator results
- ✔ Explain value types versus reference types, boxing and string equality with examples
- ✔ Trace if, switch, loops, arrays and nullable values by hand without mistakes
What .NET is, and where C# fits
The first .NET interview question is almost always "What is the CLR?" or "What is the difference between .NET Framework and .NET?", so learn this page well.
C# (said "C sharp") is a modern, object-oriented, type-safe language made by Microsoft. .NET is the platform that runs C# programs. C# is the language; .NET is the runtime, libraries and tools.
.NET has three main parts:
| Part | Full form | What it does |
|---|---|---|
| CLR | Common Language Runtime | Runs your code: loads it, JIT-compiles it, manages memory with the garbage collector, handles exceptions and security checks |
| BCL | Base Class Library | Ready-made classes: Console, String, Math, List<T>, file and network classes |
| SDK | Software Development Kit | Tools to build programs: the C# compiler and the dotnet command-line tool |
.NET Framework versus .NET (Core)
The original .NET Framework runs only on Windows. Microsoft later built .NET Core, which is open source and cross-platform (Windows, Linux, macOS). From version 5 onward the name became simply .NET. New projects use modern .NET; older company projects may still use .NET Framework.
Question: "Is LINQ part of C# or part of .NET?"
The query keywords (
from,where,select) are C# language features.The methods that do the work (
Where,Select) live in a .NET library.Answer: both — the syntax is C#, the implementation is in the .NET libraries.
From C# source to running code: IL and JIT
"What is IL?" and "What is JIT?" are standard fresher questions, and they also explain why .NET programs can run on many machines.
C# is not compiled straight to machine code. It goes through two steps.
- Compile. The C# compiler (Roslyn) turns your code into IL — Intermediate Language, also called CIL or MSIL — stored in an assembly (
.dllor.exe) together with metadata describing every type and method. - Run. The CLR loads the assembly, and the JIT (Just-In-Time) compiler turns each method's IL into native code the first time it is called. Later calls reuse that native code.
flowchart LR A[Program.cs<br/>C# source] -->|C# compiler| B[Assembly .dll<br/>IL + metadata] B -->|dotnet run| C[CLR loads assembly] C --> D[JIT compiles method<br/>on first call] D --> E[Native machine code] E --> F[Output]
CTS, CLS and managed code
- CTS (Common Type System) defines the types every .NET language shares. C#'s
intand VB.NET'sIntegerboth map to the same CTS type,System.Int32. - CLS (Common Language Specification) is a smaller rule set; a CLS-compliant library works from any .NET language.
- Managed code runs under the CLR; unmanaged code (such as a native C++ DLL) runs outside it.
- The garbage collector (GC) frees heap objects that nothing points to any more, so you do not call
freeordeleteyourself.
Code:
int fare = "250";The compiler sees a
stringbeing put into anint.Answer: compile time — no assembly is produced.
A method runs for the first time, so the JIT must compile its IL.
The second call reuses the native code already made.
Answer: the first call pays a small JIT cost; later calls do not.
What does the C# compiler produce from your .cs files?
The C# compiler emits IL plus metadata into a .dll or .exe assembly; the JIT turns IL into native code at run time.
Your first C# program
Interviewers ask you to write tiny programs on paper, so you must know the exact shape of a C# file.
Here is the classic form of a console program:
using System;
namespace HelloApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Namaste, ");
Console.WriteLine("Priya");
Console.WriteLine(args.Length);
}
}
}
Run with no command-line arguments, it prints:
Namaste, Priya
0
using System;lets you writeConsoleinstead ofSystem.Console;namespacegroups related classes.Mainis the entry point. It must bestatic. Valid forms includestatic void Main(),static int Main(string[] args)andstatic async Task Main().Console.Writeprints without a new line;Console.WriteLineadds a new line at the end.argsholds command-line arguments; with none, its length is 0, not null.
Top-level statements
Since C# 9 you can skip the class and Main completely. The compiler creates them for you:
Console.WriteLine("Train 12951 leaves at 17:00");
New console projects also turn on implicit usings, so common namespaces such as System are already imported.
The dotnet command line
| Command | What it does |
|---|---|
dotnet new console -n Billing |
Creates a new console project named Billing |
dotnet build |
Compiles the project to an assembly |
dotnet run |
Builds (if needed) and runs it |
Console.Write("A"); Console.Write("B"); Console.WriteLine("C"); Console.Write("D");Writekeeps the cursor on the same line,WriteLineends the line after C.Answer: "ABC" on the first line and "D" on the second.
class Program { void Main() { } }—Mainis not static.The compiler cannot find a valid static entry point.
Answer: it builds as a library but fails as a console app — "no suitable static Main".
Keep going with the full course
Day 1 has 23 pages. You have read the first 3.
See what's inside →