Getting Started with C#
An introduction to C# programming language and its fundamentals
What is C#?
C# (pronounced "C-Sharp") is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It combines the power of C++ with the simplicity of Visual Basic, making it an excellent choice for beginners and experienced developers alike.
Key Features
C# offers several powerful features that make it a popular choice for software development:
- Type Safety: Strong typing system prevents many common programming errors
- Memory Management: Automatic garbage collection handles memory allocation and deallocation
- Object-Oriented: Full support for classes, inheritance, polymorphism, and encapsulation
- Cross-Platform: Runs on Windows, macOS, and Linux with .NET Core/.NET 5+
Your First C# Program
Let's start with the classic "Hello, World!" program:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}Breaking Down the Code
Let's examine each part of this program:
using System; // Imports the System namespaceThe using System; directive allows us to use classes from the System namespace without fully qualifying them.
namespace HelloWorld // Defines a namespace
{
// Code goes here
}Namespaces help organize code and prevent naming conflicts.
class Program // Defines a class
{
// Class members go here
}Every C# program must have at least one class.
static void Main(string[] args) // Entry point of the program
{
Console.WriteLine("Hello, World!"); // Prints to console
}The Main method is where program execution begins.
Modern C# Syntax (C# 9+)
With newer versions of C#, you can write even simpler programs:
using System;
Console.WriteLine("Hello, World!");This top-level program feature eliminates the need for explicit namespace, class, and Main method declarations for simple programs.
Setting Up Your Environment
Installation Options
npm install dotnet --versionTo get started with C#, you'll need:
- .NET SDK: Download from dotnet.microsoft.com
- Code Editor: Visual Studio, Visual Studio Code, or JetBrains Rider
- Terminal/Command Prompt: For running dotnet commands
Creating a New Project
# Create a new console application
dotnet new console -n MyFirstApp
# Navigate to the project directory
cd MyFirstApp
# Run the application
dotnet runBasic Syntax Rules
C# follows specific syntax rules that you should remember:
Case Sensitivity
C# is case-sensitive, meaning Variable and variable are different identifiers.
Semicolons
Most statements in C# must end with a semicolon (;).
Console.WriteLine("This statement ends with a semicolon"); // Correct
Console.WriteLine("This statement also needs one") // Error: missing semicolonCurly Braces
Code blocks are enclosed in curly braces { }.
if (condition)
{
// Code block
Console.WriteLine("Inside the if block");
}Comments
Comments help document your code and are ignored by the compiler:
// This is a single-line comment
/*
This is a multi-line comment
that can span multiple lines
*/
/// <summary>
/// This is an XML documentation comment
/// Used for generating API documentation
/// </summary>
public class MyClass
{
// Class implementation
}Next Steps
Now that you understand the basics of C# syntax and structure, you're ready to explore:
- Variables and Data Types: Learn about storing and manipulating data
- Control Structures: Master if statements, loops, and decision-making
- Methods and Functions: Organize your code into reusable blocks
- Object-Oriented Programming: Dive deep into classes and objects
Each of these topics will build upon what you've learned here, gradually expanding your C# programming skills.
Last updated on