š Full lesson with practice questions on DevLadder (free, lesson 2 of 38 in the C# Language: Basics to Advanced course)
Classic program structure (what every C# file looked like before C# 9, and what top-level statements generate for you):
using System; // import a namespace so you can write Console instead of System.Console
namespace Divakar.Shop // logical grouping of types; usually matches the folder
{
internal class Program // every method lives inside a type
{
static void Main(string[] args) // entry point: static, named Main, returns void or int (or Task/Task<int>)
{
Console.WriteLine("Hello, Divakar!");
Console.Write("Your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Welcome {name}"); // string interpolation
}
}
}
Modern equivalent (C# 10+):
// GlobalUsings.cs: applies to every file in the project
global using System.Text.Json;
// Customer.cs: file-scoped namespace saves one level of indentation
namespace Divakar.Shop.Models;
public class Customer { public string Name { get; set; } = ""; }
// Program.cs: top-level statements: the compiler creates Program and Main
using Divakar.Shop.Models;
var c = new Customer { Name = "Asha" };
Console.WriteLine(JsonSerializer.Serialize(c));
Namespaces:
- Prevent name clashes (
Divakar.Shop.Models.OrdervsDivakar.Billing.Order). -
usingimports a namespace;using Alias = Some.Long.Namespace.Type;creates an alias;using static System.Math;lets you writeSqrt(2). - Implicit usings (
<ImplicitUsings>enable</ImplicitUsings>in the .csproj) auto-importSystem,System.Linq,System.Collections.Generic,System.IO,System.Threading.Tasksand more. - A namespace is not an assembly: one assembly (DLL) can contain many namespaces and one namespace can span many assemblies.
Keywords are reserved words with special meaning: class, int, if, return, public, static, new, this, base, null, true... You can't use them as identifiers unless prefixed with @ (int @class = 5;). Contextual keywords are only special in certain places and can otherwise be used as names: var, async, await, get, set, init, value, yield, record, required, where, select, field (C# 14).
Naming conventions: PascalCase for types, methods, properties and namespaces; camelCase for locals and parameters; _camelCase for private fields; interfaces start with I (IRepository); async methods end with Async.
Interview questions on C# Language
What is the difference between a value type and a reference type?
A value type variable holds the data itself (int, bool, decimal, DateTime, any struct/enum). Assigning it copies the value. A reference type variable holds a reference to an object on the managed heap (class, string, arrays, delegates, record). Assigning it copies the reference, so two variables can point to the same object.
var a = new List<int>(); var b = a; b.Add(1); // a.Count == 1: same object
int x = 1; int y = x; y++; // x is still 1
Value types are stored inline (stack for locals, inside the parent object for fields); reference-type objects always live on the heap and are cleaned up by the GC.
What is the difference between string and StringBuilder?
string is immutable: every concatenation creates a new string object, so building a string in a loop causes many allocations (O(n²) copying). StringBuilder keeps a mutable buffer and appends in place; call ToString() once at the end. Use StringBuilder for loops or many appends, plain strings/interpolation for a handful of pieces.
Explain const vs readonly vs static readonly.
-
const: compile-time constant, implicitly static, only primitives/strings; the value is baked into callers at compile time. -
readonly: instance field assigned at declaration or in a constructor; can differ per object. -
static readonly: one value per type, computed at runtime (e.g.static readonly HttpClient Client = new()).
Gotcha: if library A has public const int Max = 10 and app B uses it, changing it to 20 in A does nothing in B until B is recompiled. Use static readonly for values that may change.
Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.
Next in the course: Data types, numbers and type conversion ā
Top comments (0)