What C# and .NET are
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
C# is a modern, type-safe programming language, and .NET is the free platform it runs on. Follow a three-line program from Program.cs to the console: the compiler Roslyn checks it and turns it into IL inside an assembly, Hello.dll; then the CLR JIT-compiles that IL into machine code for your CPU, keeps new objects on the heap and lets the garbage collector free them.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What does the C# compiler (Roslyn) write into Hello.dll?
Roslyn turns C# into IL, plus metadata that describes the types and methods. Machine code is made later, at run time, by the JIT.
02When does the JIT compiler turn a method's IL into machine code?
JIT means just in time: the CLR compiles each method's IL to machine code the first time it is called, then keeps that code for the next call. A CPU only runs machine code.
03Why does the line msg * 2 (where msg is a string) never get to run?
Every value in C# has a type. The compiler checks every use of every type before anything runs, so the mistake is caught at build time (error CS0019).
04Program.cs has no class and no Main method. How does the program know where to start?
With top-level statements (C# 9 and later) the file's statements are the program. The compiler generates the class and entry-point method for you.
05After Console.WriteLine(msg), nothing uses the string msg any more. What happens to its memory?
New objects live on the heap, which the CLR manages. The garbage collector frees objects that nothing can reach, so you never free memory yourself. It runs when the CLR decides, not at a fixed line.
المسرد
- C#
- A modern, type-safe programming language from Microsoft, standardised as ECMA-334. Say "see sharp".
- .NET
- The free, open-source platform C# runs on, for Windows, macOS and Linux. It includes the dotnet tool, the CLR and a large library.
- Type-safe
- Every value has a type (such as string or int), and the compiler stops code that uses a value as the wrong type.
- Compiler (Roslyn)
- The program that checks your C# and turns it into IL. Roslyn is the name of the C# compiler.
- IL (intermediate language)
- Simple, CPU-independent instructions such as ldstr and call, stored in an assembly. Also called CIL.
- Assembly
- The file the compiler writes, such as Hello.dll: IL plus metadata that describes its types and methods.
- CLR (common language runtime)
- The part of .NET that loads assemblies, JIT-compiles IL to machine code, runs it and manages memory.
- JIT and garbage collector
- The JIT (just-in-time compiler) turns a method's IL into machine code the first time it runs; the garbage collector frees heap objects that nothing uses any more.
الملاحظات والمصادر
- The IL card is shortened but in the real order. The full Debug listing also stores and loads the two locals (stloc, ldloc), has a few nop instructions, and names each method in full, such as System.String::Concat(string, string).
- For top-level statements the compiler generates a Program class with a hidden entry-point method; you never see or write it.
- Real .NET uses tiered compilation: a quick first compile, then a better one for methods that run often. Much of the .NET library ships already compiled (ReadyToRun), so the JIT mostly compiles your own methods.
- The garbage collector runs when the CLR decides, usually when new objects need room, not at a fixed line. A program this small may end before any collection happens; when a process ends, the operating system takes back all its memory.
- Console works without a "using System;" line because new console projects turn on implicit usings (.NET 6 and later).
- The program is real C#. Put the three lines in Program.cs and dotnet run prints Hello, World.
- Microsoft Learn: A tour of the C# language
- Microsoft Learn: Managed execution process (C# to IL to machine code)
- Microsoft Learn: Common Language Runtime (CLR) overview
- Microsoft Learn: Top-level statements
- Microsoft Learn: dotnet new and dotnet run
- Microsoft Learn: Console.WriteLine method
- Microsoft Learn: Fundamentals of garbage collection
- Microsoft Learn: Runtime configuration options for compilation (tiered compilation)
- ECMA-335: Common Language Infrastructure (CLI)
- ECMA-334: C# Language Specification
اختبار يتاح بعد الخطوة الأخيرة.