designwithfocus

Programming languages

What Is C#? The Language .NET Apps Are Written In

By Design With Focus Team 6 min read
A short C# source file next to a run button

Short answer

C# is a strongly typed, cross-platform language used to write apps that run on the .NET platform. Microsoft's tour calls it the most popular language for that free, open-source platform. A program is compiled, then run, often with dotnet build and dotnet run. The runtime's garbage collector reclaims objects the program is no longer using.

Last verified against the sources listed below.

Key takeaways

  • C# targets .NET, and the tour calls it the most popular language on that platform.
  • Statements end with semicolons. Names are case-sensitive. Braces group blocks.
  • dotnet build compiles source. dotnet run compiles if needed, then runs the program.
  • File-based apps, from C# 14 and .NET 10, can live in a single .cs file.
  • The garbage collector frees unused managed objects. Unmanaged handles still need Dispose.

A direct answer to what is c# is the language Microsoft documents for .NET apps. The tour calls it a cross-platform, general-purpose language and the most popular language for the .NET platform, which it describes as free, cross-platform, and open source.

The tour of C# says programs can run on phones, desktops, laptops, and servers, and on devices from small hardware to the cloud. The .NET language page calls C# a modern, open-source, cross-platform, object-oriented language, and the most popular language for .NET development.

How does a program start?

A program can start as one line. The tour’s first example prints a line with the Console class:

Console.WriteLine("Hello, World");

The line that starts with two slashes is a comment. A single-line comment runs to the end of the line. A multi-line comment starts with a slash and a star and ends with a star and a slash. WriteLine is a method of Console, in the System namespace.

The standard libraries are referenced by default.

An older shape of the same program declares a class and a static Main method. The tour says both forms compile to the same code. With top-level statements, the compiler supplies the class and the entry point. Semicolons end statements.

Identifiers are case-sensitive. Braces group blocks, as in if, else, switch, for, while, and foreach.

How do you compile and run it?

You compile the source, then the runtime runs the result. The tour says C# is a compiled language. In most programs you use dotnet build to compile source files into a binary package, and dotnet run to run it. dotnet run compiles first when that is needed.

The dotnet CLI ships in the .NET SDK.

Beginning with C# 14 and .NET 10, a file-based app can be one .cs file. The tour says you run it with dotnet run and the filename. A shebang line can let a Unix system execute that file directly when the execute permission is set. The source must be a single file.

The rest of the language is still available. The tour suggests this form for small command-line tools, prototypes, and experiments.

Python is a different language with a different runtime.

A programming paradigm is the style of the code, such as the object-oriented style the tour says C# is based on, plus functional features it also includes.

What types does the language include?

The language includes types the compiler checks before the program runs. The tour says C# is strongly typed. Every variable has a type known at compile time. Fundamental types include int, double, and char, and reference types such as string, arrays, and other collections.

You define your own struct types for values and class types for object-oriented behavior. The record modifier can be added to either so the compiler writes equality code. An interface is a contract a type must implement. Generics use a type parameter as a placeholder.

Methods can be overloaded and can return a value. Types can also have properties and events.

The tour shows two later forms in short samples. async and await mark a method that waits on work such as an HTTP request and then continues. A LINQ query can filter a collection with from, where, and select. Pattern matching can branch on the shape of a value.

Those are language features, not separate products.

What is memory leakage in C#?

The runtime collects unused managed objects, which removes one common way to leak memory. The tour says C# apps benefit from the runtime’s automatic memory management. Microsoft’s garbage collection page says that automatic management can eliminate forgetting to free an object and causing a memory leak, and also the mistake of using memory that was already freed.

The collector allocates objects on the managed heap and later reclaims objects the application is no longer using. By default, on 32-bit computers, each process has a 2-GB user-mode virtual address space. The page says you work with that virtual space. The collector allocates and frees virtual memory on the managed heap for you.

The managed heap is split into generations 0, 1, and 2. New objects start in generation 0. Objects that survive are promoted. Objects that are 85,000 bytes and larger usually go on the large object heap, which the page says is often arrays.

Generation What the garbage collection page says
0 Youngest objects. Collection happens here most often.
1 A buffer between short-lived and long-lived objects.
2 Long-lived objects. Survivors of this collection stay in generation 2.

A collection can run when physical memory is low, when allocations pass a threshold, or when code calls GC.Collect. The page says you almost never need to call that method.

Unmanaged resources are the leftover case. The page names a file handle, a window handle, or a network connection. The collector can track the managed wrapper and does not know how to release the resource. You provide a public Dispose method and call it when you are finished.

A finalizer or a safe handle is the backup if a caller forgets.

Who writes the language rules?

Ecma International publishes a specification, and Microsoft documents the current language and the SDK. ECMA-334 is the C# language specification. The page lists the 7th edition, December 2023. It describes the representation of programs, the syntax and constraints, the semantic rules, and the limits on a conforming implementation.

The same page says the specification does not describe how a program is transformed for a machine, how an application is started, or how input and output are transformed. That is why the tour, not the Ecma page, is the place that names dotnet build and dotnet run.

The csharplang repository is described there as the official repo for the design of the C# programming language. Design notes live with that repo. The shipping compiler and libraries ship with the .NET SDK the tour tells you to use.

The Word Counter counts words in docs and in UI strings. It does not compile C#. It is a useful check before a string is hard-coded into a label, a document, or a test, which is a different job from the type checker the tour describes.

Frequently asked questions

Is C# the same language as C or C++?

No. Microsoft's tour places C# in the C family, so the syntax feels familiar if you have used C, C++, JavaScript, TypeScript, or Java. Semicolons end statements, identifiers are case-sensitive, and braces, if, else, switch, for, and while look similar. C# also has foreach for any collection. The tour says the language is based on object-oriented principles and also includes functional features.

How does a C# program run?

It is compiled, then run on .NET. The tour says you usually use dotnet build to compile source files into a binary package, and dotnet run to run it. dotnet run compiles first if that is necessary. Beginning with C# 14 and .NET 10, a file-based app can be a single .cs file that you start with dotnet run and the filename.

What is memory leakage in C#?

Microsoft's garbage collection fundamentals say automatic memory management can eliminate the common problem of forgetting to free an object and causing a memory leak. The collector finds objects the application is no longer using and reclaims their memory. Managed code does not ask you to free those objects by hand. Unmanaged resources are different. A file handle, window handle, or network connection still needs an explicit Dispose method.

What types does the tour say are built in?

The tour says C# is strongly typed, so every variable has a type known at compile time. Fundamental types built into the language and runtime include value types such as int, double, and char, and reference types such as string, plus arrays and other collections. The compiler can flag a type mistake before the program runs.

Does the Ecma specification describe the .NET runtime?

Not the parts the Ecma page lists as out of scope. ECMA-334, 7th edition, December 2023, describes the form of C# programs, the syntax and constraints, the semantic rules, and the restrictions on a conforming implementation. The same page says the specification does not describe how programs are transformed for a data-processing system, or how applications are invoked.

Sources