Introduction to C#
Welcome to the C# basics tutorial, part of the comprehensive MSDN .NET documentation. This guide will introduce you to the fundamental concepts of the C# programming language, enabling you to start building your own applications.
What is 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 desktop applications to web services and mobile apps.
Key Features of C#:
- Object-Oriented: C# supports encapsulation, inheritance, and polymorphism, allowing for modular and reusable code.
- Type-Safe: The language enforces strict type checking at compile-time, helping to prevent many common programming errors.
- Managed Code: C# code runs within the Common Language Runtime (CLR), which provides services like memory management (garbage collection) and exception handling.
- Versatile: It can be used for developing Windows applications, web applications (ASP.NET), mobile applications (Xamarin), cloud services, games (Unity), and more.
- Modern Syntax: C# has a clear and expressive syntax, making it relatively easy to learn and use.
Getting Started
To begin writing C# code, you'll need a development environment. The most popular choice is Visual Studio, a powerful Integrated Development Environment (IDE) from Microsoft. You can also use Visual Studio Code with the C# extension for a lighter-weight experience, or the .NET SDK directly from the command line.
Your First C# Program: "Hello, World!"
Let's write a simple program to display "Hello, World!" on the console.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
When you run this code, it will output:
Hello, World!
Explanation of the Code:
using System;
: This line imports theSystem
namespace, which contains fundamental classes likeConsole
.public class HelloWorld
: This defines a class namedHelloWorld
. In C#, all code resides within classes.public static void Main(string[] args)
: This is the entry point of your program. When you run the application, the code inside theMain
method is executed first.Console.WriteLine("Hello, World!");
: This line uses theWriteLine
method from theConsole
class to print the string "Hello, World!" to the standard output (usually your terminal or console window).
Next Steps
Now that you've seen a basic C# program, you're ready to dive deeper. The next tutorial will cover Variables and Data Types, which are essential for storing and manipulating data in your programs.
The journey of a thousand miles begins with a single step.