An Introduction to Microsoft's Modern, Object-Oriented Programming Language
C# (pronounced "C sharp") is a modern, object-oriented, and type-safe programming language developed by Microsoft within the .NET initiative. It combines the productivity of a rapid application development language with the power and flexibility of a compiled language. C# is designed for building a wide variety of applications on the .NET platform, from desktop and web applications to mobile apps, games, and enterprise-level services.
This document provides a foundational understanding of C#, its key features, and how it is used to develop powerful software solutions.
To start writing C# code, you'll need the .NET SDK. You can download it from the official .NET download page.
Here's a simple "Hello, World!" program to get you started:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
You can compile and run this code using the .NET CLI:
dotnet new console -o MyHelloWorldApp
cd MyHelloWorldApp
dotnet run
C# is statically typed, meaning you must declare the type of a variable. Common data types include:
int (integers)double (floating-point numbers)bool (true/false values)string (text)char (single characters)int count = 10;
double price = 99.99;
bool isActive = true;
string message = "Welcome!";
C# provides standard control flow statements:
if, else if, else for conditional execution.for, while, do-while, foreach for loops.switch for multi-way branching.As an object-oriented language, C# uses classes as blueprints for creating objects. Objects encapsulate data (fields or properties) and behavior (methods).
public class Dog
{
public string Name { get; set; }
public string Breed { get; set; }
public void Bark()
{
Console.WriteLine($"{Name} says Woof!");
}
}
// Usage:
Dog myDog = new Dog { Name = "Buddy", Breed = "Golden Retriever" };
myDog.Bark();
Methods are blocks of code that perform a specific task. They can accept parameters and return values.
Namespaces are used to organize code and prevent naming conflicts. The using directive allows you to access types within a namespace.
C# is the primary language for the .NET platform, which includes:
The .NET ecosystem is constantly evolving, with frequent updates and new features being released.
This overview provides a glimpse into the world of C#. To dive deeper, explore these resources: