Introduction to C# and .NET
# CHAPTER 1
Introduction to C# and .NET
If your impression of C# is "Microsoft's Java, for Windows desktop software," it is about a decade out of date.
C# arrived in 2000 as the flagship language of Microsoft's .NET platform, and for its first fifteen years that description was broadly fair — it was a capable, statically typed, object-oriented language locked to Windows. Then in 2016 Microsoft released .NET Core: open source, cross-platform, and running natively on Linux and macOS. That single decision moved C# from a Windows language to a general-purpose one, and .NET has since unified back into a single cross-platform runtime.
What you can build with it now is unusually broad for one language. Web APIs and applications with ASP.NET Core, which benchmarks among the fastest mainstream web frameworks available. Cross-platform mobile and desktop apps with MAUI. Cloud services on Azure or anywhere else. And a very large share of the commercial games industry, because Unity uses C# as its scripting language.
The language itself has aged well, absorbing ideas thoughtfully rather than accumulating them: LINQ for querying collections, async/await for asynchronous code — which C# introduced and JavaScript, Python and Rust subsequently adopted — and increasingly capable pattern matching.
This chapter separates three things beginners routinely conflate: the C# language, the .NET runtime that executes it, and the SDK you build with. Getting that distinction straight now prevents most early confusion.
1. What is C#?
C# is a modern, object-oriented, and type-safe programming language created by Microsoft in 2000, led by Anders Hejlsberg. It was heavily influenced by C++ and Java. C# enables developers to build secure and robust applications that run on the .NET ecosystem.Real-World Analogy: If building an application is like building a house, C# is the set of high-quality tools (hammers, drills, saws) you use, and .NET is the foundation, plumbing, and electricity pre-installed on the plot of land for you to build upon.
2. What is .NET?
.NET (pronounced "dot-net") is a free, cross-platform, open-source developer platform for building many different types of applications.- Originally called the .NET Framework (Windows only).
- Evolved into .NET Core (Cross-platform: Windows, Linux, macOS).
- Now simply called .NET (Unified platform starting from .NET 5).
3. The CLR and .NET Runtime
Unlike C++ which compiles directly to machine code, C# compiles to an intermediate language.- 1. You write C# source code.
- 2. The compiler translates it into Intermediate Language (IL).
- 3. The Common Language Runtime (CLR) takes this IL and translates it into machine code that the specific operating system understands using a Just-In-Time (JIT) compiler.
4. Features of C#
- 1. Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
- 2. Type-Safe: Prevents memory corruption by strictly enforcing data types.
- 3. Garbage Collected: Automatically cleans up unused memory, preventing memory leaks.
- 4. Modern: Supports async/await, lambdas, LINQ, and pattern matching.
- 5. Cross-Platform: Write once, run on Windows, Linux, and Mac.
5. Applications of C#
- Web Development: ASP.NET Core powers massive enterprise websites.
- Game Development: The Unity Game Engine uses C# as its primary scripting language.
- Desktop Apps: Windows Presentation Foundation (WPF) and Windows Forms.
- Mobile Apps: Xamarin and .NET MAUI for iOS/Android apps.
- Cloud/Backend: Azure microservices and highly scalable REST APIs.
6. Mini Project: Welcome Console Application
Here is a sneak peek at what your first C# program will look like:7. OOP and Memory Explanation
In C#, memory is divided into the Stack and the Heap. When the program runs, the CLR manages this memory. Variables are allocated on the Stack, while Objects (like Strings or custom Classes) are allocated on the Heap. The Garbage Collector runs periodically in the background to delete Heap objects that are no longer being used, saving you from doing it manually.8. Common Mistakes
- Confusing C# with C++: C++ requires manual memory management and compiles directly to hardware. C# runs inside the managed CLR environment.
- Thinking .NET is only for Windows: While true 15 years ago, modern .NET is fully cross-platform and runs beautifully on Linux servers.
9. Best Practices
- Always capitalize the 'S' in C#. It is not C-hash or C-pound.
- Understand the difference between the language (C#) and the framework (.NET).
10. Exercises
- 1. Research and list three popular video games built using C# and Unity.
- 2. Explain the difference between source code and IL code in your own words.
11. MCQs with Answers
Who created C#?
Which of the following handles automatic memory management in .NET?
C# source code is initially compiled into what?
Which game engine relies primarily on C#?
What does JIT stand for?
Which framework is used for building web applications in C#?
C# is a type-safe language. What does this mean?
12. Interview Questions
- Q: Explain the C# compilation process. What role does the JIT compiler play?
- Q: What is the difference between Managed Code and Unmanaged Code?
- Q: Describe the role of the Garbage Collector in .NET.