C# Documentation
On this page:
Introduction to C#
C# (pronounced "C sharp") is a modern, object-oriented, and type-safe programming language developed by Microsoft. It is designed for building a wide range of applications on the .NET platform, from web applications and services to desktop software, mobile apps, and games.
C# is a versatile language that combines the productivity of a high-level language with the power and performance of a low-level language. It has a rich set of features that make it suitable for developing robust and scalable software.
Key Features of C#
- Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
- Type-Safe: Helps prevent type errors at compile time.
- Component-Oriented: Facilitates the creation of reusable software components.
- Modern: Includes features like LINQ, async/await, and pattern matching.
- Managed: Runs in a managed execution environment (CLR) that provides memory management and other services.
- Versatile: Can be used for web, desktop, mobile, cloud, and IoT development.
Getting Started with C#
To start writing C# code, you'll need a development environment. The most popular choice is Visual Studio or Visual Studio Code with the C# extension.
- Install .NET SDK: Download and install the latest .NET SDK from the official .NET website.
- Create a Project: Use the .NET CLI to create a new project:
dotnet new console -o MyCSharpApp cd MyCSharpApp
- Write Code: Open the
Program.cs
file and start coding. - Run: Compile and run your application:
dotnet run
For a more in-depth guide, check out the Getting Started guide.
Code Examples
"Hello, World!" Program
A simple example demonstrating the basic structure of a C# console application.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Basic Class Definition
Illustrates how to define a simple class and create an object from it.
using System;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Greet()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
class Example
{
static void Main(string[] args)
{
Person person1 = new Person("Alice", 30);
person1.Greet();
}
}