Introduction to C#
Welcome to the world of C#! This guide will introduce you to the fundamental concepts and syntax of the C# programming language, empowering you to start building powerful applications on the .NET platform.
What is C#?
C# (pronounced "C sharp") is a modern, object-oriented programming language developed by Microsoft. It's designed to be a simple, general-purpose, type-safe, and object-oriented language. C# is a versatile language used for developing a wide range of applications, including:
- Web applications and services (ASP.NET)
- Desktop applications (Windows Forms, WPF)
- Mobile applications (Xamarin)
- Game development (Unity)
- Cloud services
- Internet of Things (IoT) devices
Key Features of C#
C# offers several compelling features that make it a popular choice for developers:
- Object-Oriented: Supports classes, objects, inheritance, polymorphism, and encapsulation.
- Type-Safe: The compiler checks for type compatibility, reducing runtime errors.
- Managed Code: Executed within the .NET Common Language Runtime (CLR), which provides features like automatic memory management (garbage collection).
- Component-Oriented: Designed for building reusable software components.
- Modern Syntax: Influenced by C++, Java, and other languages, making it relatively easy to learn.
- Rich Standard Library: The .NET Framework/Core provides a vast collection of pre-built classes and functionalities.
Your First C# Program: "Hello, World!"
Let's start with the traditional "Hello, World!" program. This simple program demonstrates the basic structure of a C# application.
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Explanation:
using System;
: This line imports the `System` namespace, which contains fundamental classes like `Console`.public class HelloWorld
: This declares a class named `HelloWorld`. In C#, all code resides within classes.public static void Main(string[] args)
: This is the entry point of the application. The program execution begins here.public
: An access modifier, meaning the method can be accessed from anywhere.static
: The method belongs to the `HelloWorld` class itself, not to any specific instance of the class.void
: The method does not return any value.Main
: The name of the entry point method.string[] args
: An array of strings that can be used to pass command-line arguments to the application.
Console.WriteLine("Hello, World!");
: This line uses the `WriteLine` method from the `Console` class to print the string "Hello, World!" to the console, followed by a new line.
HelloWorld.cs
, compile it using the command line (e.g., csc HelloWorld.cs
), and then run the executable. Alternatively, you can use an IDE like Visual Studio or VS Code.
Next Steps
Congratulations on writing your first C# program! In the next tutorials, we'll dive deeper into essential C# concepts such as variables, data types, operators, and control flow structures. Keep exploring and happy coding!