Knowledge / Computer Applications / C# and .NET basics

C# and .NET basics

Computer Applications (BCA) · Programming · free study notes for exams, placements and interviews

In short

C# is a statically typed, object-oriented, managed language created by Microsoft (Anders Hejlsberg, 2000) that compiles to Intermediate Language (IL) and runs on the .NET runtime. .NET is the platform around it - a runtime (CLR), a Base Class Library, and tooling (dotnet CLI, NuGet, Visual Studio) - and since .NET Core it is cross-platform and open source. BCA and MCA syllabi examine C# and .NET as a full paper (CLR architecture, value vs reference types, delegates, LINQ, ADO.NET, ASP.NET), and placement tests ask it in the same breath as Java. It is a mainstream hiring skill in India - TCS, Infosys, Wipro, Cognizant, Accenture, Capgemini, LTIMindtree, HCLTech and most BFSI/insurance product teams run large .NET estates, and ASP.NET Core Web API plus SQL Server is a standard fresher job description.

Explained

.NET architecture: CLR, CTS, CLS, assemblies

C# language core

OOP in C#, as C# specifically does it

Memory, GC and resource disposal

Collections, generics, LINQ and exceptions

The .NET application stack used in Indian jobs

Key points

Formulas and examples

C# and .NET has no numeric formulas; the equivalents are the definitional relations and complexity costs below.

Execution path: C# source → (Roslyn csc) → MSIL + metadata in an assembly → (CLR JIT or AOT) → native code
Lookup cost: List<T>.Contains = O(n); Dictionary<K,V>[key] / HashSet<T>.Contains = O(1) average, O(n) worst
Naive string concatenation in a loop of n items ≈ O(n²) characters copied; StringBuilder ≈ O(n)
Boxing cost per operation = 1 heap allocation + 1 copy of the value (size in bytes of the value type) + later GC work
sizeof: byte 1, short 2, int/float 4, long/double 8, decimal 16 bytes; char 2 bytes (UTF-16)

Example 1 - value vs reference semantics, step by step.

struct PointS { public int X; }
class  PointC { public int X; }

var s1 = new PointS { X = 10 };
var s2 = s1;              // copies the value
s2.X = 99;                // s1.X stays 10

var c1 = new PointC { X = 10 };
var c2 = c1;              // copies the reference
c2.X = 99;                // c1.X is now 99
Console.WriteLine($"{s1.X} {c1.X}");   // prints: 10 99

Step 1: s2 = s1 duplicates 4 bytes on the stack, so the two are independent. Step 2: c2 = c1 duplicates only the handle, so both names point to one heap object. Answer: 10 99.

Example 2 - string concatenation cost with real numbers. Build a 10,000-row CSV line set where each row adds 50 characters.

**Answer: use StringBuilder; the naive loop is quadratic.**

Example 3 - LINQ on a placement dataset, computed.

record Student(string Name, string Branch, int Marks, decimal Package);
var list = new List<Student> {
    new("Asha",  "BCA", 78, 450000m),
    new("Rahul", "BCA", 55, 300000m),
    new("Meera", "MCA", 91, 800000m),
    new("Vivek", "BCA", 64, 360000m),
    new("Sana",  "MCA", 48, 250000m)
};
var top = list.Where(s => s.Marks >= 60)
              .OrderByDescending(s => s.Package)
              .Select(s => new { s.Name, LPA = s.Package / 100000m })
              .ToList();
var avgBca = list.Where(s => s.Branch == "BCA").Average(s => s.Package);

**Example 4 - integer division, overflow and decimal for money.**

int a = 7, b = 2;
Console.WriteLine(a / b);              // 3   (integer division truncates)
Console.WriteLine((double)a / b);      // 3.5
Console.WriteLine(0.1 + 0.2 == 0.3);   // False (binary floating point)
Console.WriteLine(0.1m + 0.2m == 0.3m);// True  (decimal is base-10)

int big = int.MaxValue;                // 2,147,483,647
unchecked { Console.WriteLine(big + 1); }   // -2147483648 (wraps)
checked   { /* big + 1 throws OverflowException */ }

GST check with decimal: invoice base ₹1,20,000 at 18% → tax = 1,20,000 × 0.18 = ₹21,600, total ₹1,41,600. With double, repeated rounding on thousands of lines drifts; **money fields use decimal.**

**Example 5 - virtual/override vs new.**

class Base    { public virtual string Who() => "Base"; }
class Over : Base { public override string Who() => "Over"; }
class Hide : Base { public new     string Who() => "Hide"; }

Base x = new Over(); Console.WriteLine(x.Who());   // "Over"  - virtual dispatch
Base y = new Hide(); Console.WriteLine(y.Who());   // "Base"  - hidden, not overridden
Console.WriteLine(((Hide)y).Who());                // "Hide"
Aspect.NET Framework 4.8.NET (Core) 5 / 6 / 7 / 8+
PlatformWindows onlyWindows, Linux, macOS, containers
SourceClosedOpen source (MIT, GitHub)
Web stackASP.NET MVC 5 / Web FormsASP.NET Core (MVC, Web API, Blazor, minimal APIs)
DeploymentMachine-wide runtime, GACSide-by-side, self-contained, single-file, AOT
CLIMSBuild / Visual Studiodotnet CLI, cross-platform
StatusMaintained, no new featuresActive; even-numbered releases are LTS
C#Java
RuntimeCLR, IL, JIT/AOTJVM, bytecode, JIT
Value typesUser struct, record structOnly primitives (value classes still evolving)
PropertiesNative get/setGetter/setter methods
Delegates and eventsFirst-classFunctional interfaces, listeners
Operator overloadingYesNo
Query supportLINQ built inStreams API
GenericsReified (available at run time)Erased at compile time

Where it is used

Interview questions

Common mistakes

Related topics

Object-oriented programming concepts · Java essentials · JavaScript essentials · C++ essentials · Database management and SQL · Web development and REST APIs

Open in the Knowledge reader →Find jobs →