C Sharp (programming language)
Adapted from Wikipedia ยท Discoverer experience
C# is a general-purpose programming language that supports many different ways to write code. It works with many types of programming styles, including ones that focus on objects and classes, and others that use functions.
The main creators of C# were Anders Hejlsberg, Scott Wiltamuth, and Peter Golde from Microsoft. C# was first released to the public in July 2000. Soon after, it became an international standard. Microsoft created C# together with the .NET Framework and Microsoft Visual Studio.
Over time, Microsoft made many tools for C# available for free and for use on different types of computers. This made C# more accessible to programmers around the world. The most recent stable version of C# as of November 2025 is C# 14.
Design goals
C# was created to be a simple and modern programming language that helps developers write strong and reliable code. It supports checking for mistakes like using variables before they are set and automatically cleaning up unused memory to keep programs safe.
The language works well for building many types of software, from big applications running on full computers to small programs on simple devices. It is easy to move between different computers and systems, making it friendly for programmers who already know similar languages. While C# aims to use memory and processing power wisely, it does not try to match the speed of older, more complex languages.
History
During the creation of the .NET Framework, a team at Microsoft started building a new programming language called COOL, short for "C-like Object Oriented Language". Eventually, this language was renamed to C#.
The main designer of C# was Anders Hejlsberg, who previously helped create other programming tools like Turbo Pascal, Delphi, and Visual J++. He wanted C# to avoid problems found in older languages like C++, Java, and Delphi.
C# has changed a lot since it was first made. It now includes features that make writing programs easier and more fun, like LINQ, which helps organize and find information in databases or files. Over time, C# has grown in many ways to better fit the needs of programmers.
Syntax
Main article: C# syntax
See also: Syntax (programming languages)
C# uses a style of writing code that is like other languages such as C, C++, and Java. In C#, you end each line of code with a semicolon. You use curly brackets to group lines of code together, like putting them into boxes. You can give values to variables using an equals sign and compare them using two equals signs next to each other. Square brackets help you work with lists, called arrays, by showing where to find a specific item in the list. Words like "class," "int," and "void" help set up big parts of a program.
Distinguishing features
See also: Comparison of C# and Java
C# has many special features that make it different from other languages like C, C++, and Java. Some of these features include:
Portability
C# is designed to work closely with a system called the Common Language Infrastructure (CLI). This means C# can work on many different types of computers and operating systems. C# compilers can create different kinds of code, not just one specific type.
Typing
C# has strong typing, meaning it pays close attention to the types of data you use. For example, it has a special type called bool just for true or false values. Unlike C++, C# does not let you mix up numbers and true/false values, which helps prevent mistakes.
C# also has special rules to keep types safe. It only allows safe conversions by default, like making bigger numbers from smaller ones. It also supports advanced features like covariance and contravariance in generic types.
Metaprogramming
Metaprogramming lets you write code that can change or inspect other code. C# supports this in several ways:
- Reflection: This lets your program look at and use types and methods at runtime.
- Expression trees: These let you represent code as data, which can be inspected or changed while the program runs.
- Attributes: These are special tags you can add to code to give it extra information.
- The .NET Compiler Platform (Roslyn): This lets you work with code while it is being compiled.
- Source generators: These let you create new code during compilation.
Methods and functions
In C#, a method is a part of a class that can do something, like add numbers or change data. Methods can have different ways of getting and giving back values. C# also lets you add new methods to existing classes using extension methods.
Properties
C# lets you add properties to classes. Properties are like special kinds of methods that can get or set values. Since version 3.0, C# has a simpler way to create properties called auto-implemented properties.
Namespaces
Namespaces help organize code by grouping related parts together. In C#, you can use the using statement to easily bring in code from other namespaces.
Memory access
In C#, most of the time you don't have to worry about memory because the system takes care of it for you. But you can use special "unsafe" blocks if you need to work directly with memory addresses. The system also takes care of cleaning up unused memory automatically.
Exceptions
C# has a way to handle mistakes or unexpected situations in your code called exceptions. You can use a try-catch block to catch these mistakes and decide what to do instead. There is also a finally block that always runs, no matter what happens.
Polymorphism
C# supports polymorphism, which means you can write code that works with many different types in a similar way. C# does not let a class inherit from more than one other class, but it can implement many interfaces. C# also supports overloading methods and operators, and since version 2.0, it supports generic types.
Language Integrated Query (LINQ)
C# has a special feature called LINQ that lets you search and work with data in many different places, like databases or lists. LINQ makes it easier to filter, sort, and work with data.
Functional programming
C# adds more functional programming features over time. These include:
- Functions as first-class citizens
- Higher-order functions
- Anonymous functions
- Closures
- Type inference
- List comprehension
- Tuples
- Nested functions
- Pattern matching
- Immutability
- Type classes
Common type system
C# uses a unified type system called the Common Type System (CTS). This means all types, even basic ones like numbers, are versions of a main class called System.Object. For example, every type can use a method named ToString().
The CTS divides data types into two groups: reference types and value types. Value types, like numbers and dates, always have a default value and compare by what data they hold. Reference types, like strings and arrays, are unique to each piece and compare by their identity unless special rules are added. Both types can be expanded with types made by users.
Boxing changes a value type into a reference type, and this happens automatically. Unboxing changes it back, but you must tell the computer exactly what type to change it to. For example:
int foo = 42; // Value type.
object bar = foo; // foo is boxed to bar.
int foo2 = (int)bar; // Unboxed back to value type.
Libraries
The C# specification describes the basic types and class libraries that the compiler needs. Usually, C# is used with an implementation of the Common Language Infrastructure, which is a standard called ECMA-335 Common Language Infrastructure (CLI).
Besides the standard CLI, there are many extra libraries made by companies and communities. These add more features to the .NET framework. C# can also use any library listed in the List of .NET libraries and frameworks.
Examples
Hello World
Here is a very simple C# program, a version of the classic "Hello World" example using a special feature from C# 9:
Console.WriteLine("Hello, World!");
For older versions of C#, the program needs to be written inside a special method:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
This code will show the words "Hello, World!" on the screen.
Generics
Generics is a feature that helps developers create better and faster programs by letting them work with data in a safer way. Before generics, developers had to use less efficient tools to store different types of data. With generics, they can create data structures that know exactly what kind of data they hold, making the programs run faster and be easier to maintain.
Example
public class DataStore
{
private T[] items = new T;
private int count = 0;
public void Add(T item)
{
items[count++] = item;
}
public T Get(int index)
{
return items[index];
}
}
Standardization and licensing
In 2001, Microsoft, Hewlett-Packard, and Intel worked together to submit the rules for C# and the Common Language Infrastructure to a group called Ecma International. Soon after, in 2001, they released the official C# rules as ECMA-334. By 2003, C# became an official standard through ISO and IEC. Over the years, more rules and features were added, like partial classes and special types.
The rules for C# are protected to ensure fair use, and Microsoft made promises not to use certain patents against open-source projects and specific products. Later, Microsoft created free tools for C# that work on many different systems.
Implementations
Microsoft created free tools and compilers for the C# programming language. The first compiler, called Roslyn, changes C# code into a special form called intermediate language (IL). Another compiler, RyuJIT, works quickly to change IL into code that your computer can use right away. RyuJIT is free to use and written in C++. Roslyn is written using C# itself and helps developers build tools to check and improve code.
There are different versions of C# available. One version works only on Windows, while another works on many types of computers. These versions now share a common free version called .NET 5.0. Other projects, like Mono, also provide free C# compilers and tools. Tools like Elements can change C# code to work on many different systems, including games. Popular game engines like Unity and Godot use C# for creating games.
Related articles
This article is a child-friendly adaptation of the Wikipedia article on C Sharp (programming language), available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.
Safekipedia