An Introduction to C# for Beginners

A beginner's guide to the C# programming language. Learn about its key features, what it's used for, and walk through the basic syntax of variables, data types, and functions.

C# (pronounced "C-sharp") is a modern, object-oriented, and type-safe programming language developed by Microsoft. It's one of the most popular programming languages in the world, and it's the primary language for building applications on the .NET platform.

If you're looking to learn a powerful and versatile language, C# is an excellent choice.

What is C# Used For?

C# can be used to build almost anything:

  • Web Applications: With ASP.NET, you can build robust and high-performance web sites and services.
  • Windows Desktop Applications: C# is the language of choice for building applications for Windows using frameworks like Windows Forms and WPF.
  • Games: The popular Unity game engine uses C# as its primary scripting language.
  • Mobile Apps: With Xamarin, you can use C# to build cross-platform mobile applications for iOS and Android.
  • Cloud Services: C# is a first-class citizen for building applications on cloud platforms like Microsoft Azure and AWS.

Your First C# Program: Hello, World!

Let's look at a simple "Hello, World!" program in C#.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Let's break this down:

  • using System;: This includes the System namespace, which contains fundamental classes, including Console.
  • namespace HelloWorld: A namespace is a way to organize your code.
  • class Program: C# is an object-oriented language, so all code lives inside a class.
  • static void Main(string[] args): This is the entry point of the application. When the program runs, the Main method is the first method to be executed.
  • Console.WriteLine("Hello, World!");: This line writes the text "Hello, World!" to the console.

Variables and Basic Data Types

A variable is a name given to a storage location. In C#, you must declare the type of a variable before you use it. This is because C# is a statically-typed language.

Here are some of the basic data types:

// A string for text
string name = "Alice";

// An integer for whole numbers
int age = 30;

// A double for numbers with decimal points
double price = 19.99;

// A bool for true/false values
bool isActive = true;

Working with Data: Arrays and Lists

Arrays

An array is a collection of a fixed number of items of the same type.

int[] numbers = new int[5]; // An array of 5 integers
numbers[0] = 10;
numbers[1] = 20;

string[] names = { "Bob", "Charlie" };

Lists

A List<T> is a more flexible collection that can grow and shrink in size. It's part of the generic collections library.

using System.Collections.Generic;

List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");

Controlling the Flow: if Statements and for Loops

if Statements

An if statement executes code based on a boolean condition.

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    Console.WriteLine("You are not an adult.");
}

for Loops

A for loop is used to execute a block of code a specific number of times.

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

Reusing Code: Methods

A method (often called a function in other languages) is a block of code that performs a specific task. You can define methods to reuse code.

public class Greeter
{
    public string GetGreeting(string name)
    {
        return "Hello, " + name;
    }
}

// To use it:
var greeter = new Greeter();
string message = greeter.GetGreeting("Bob");
Console.WriteLine(message); // Output: Hello, Bob

Conclusion

C# is a powerful, modern, and versatile programming language. This guide has only scratched the surface of its capabilities. With its strong typing, object-oriented features, and the power of the .NET platform behind it, C# is a fantastic language for building robust and scalable applications of all kinds.