A quick peek to C# language

C# (pronounced “C-sharp”) is a modern, object-oriented programming language developed by Microsoft. It’s commonly used for building applications ranging from web, desktop, and mobile apps to game development.

Here’s a quick introduction to some of the core concepts in C#.


1. Setting Up

To get started with C#, you’ll need:

  • Visual Studio (IDE): Download and install Visual Studio Community Edition.
  • .NET SDK: It comes with Visual Studio, but you can also download it separately.

Create a new project by selecting “Console App” from the templates.


2. Hello World

The basic “Hello, World!” program in C# looks like this:

using System;

namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
  • using System: This imports the System namespace, which contains basic functionality like Console.
  • Main is the entry point of a C# program.
  • Console.WriteLine prints text to the console.

3. Variables and Data Types

C# supports various data types, such as:

  • int for integers
  • double for floating-point numbers
  • string for text
  • bool for true/false values

Example:

int age = 30;
double height = 5.9;
string name = "Alice";
bool isStudent = true;

Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}, Is Student: {isStudent}");

4. Control Flow: If-Else

Use if-else statements to control the flow of your program based on conditions.

int age = 20;

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

5. Loops

C# provides several loop constructs, including for, while, and foreach.

for loop:

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

while loop:

int count = 0;
while (count < 5)
{
    Console.WriteLine($"Count: {count}");
    count++;
}

foreach loop:

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

foreach (string name in names)
{
    Console.WriteLine(name);
}

6. Functions (Methods)

Functions in C# help break your code into reusable blocks.

static void Greet(string name)
{
    Console.WriteLine($"Hello, {name}!");
}

static void Main(string[] args)
{
    Greet("Alice");
    Greet("Bob");
}
  • void means the method does not return a value.
  • You can pass parameters to methods, like name in the example.

7. Classes and Objects

C# is object-oriented, so everything revolves around classes and objects.

Defining a class:

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void Introduce()
    {
        Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
    }
}

Creating an object:

Person person = new Person();
person.Name = "Alice";
person.Age = 25;
person.Introduce();

8. Arrays and Lists

C# supports arrays and collections like List<T>.

Arrays:

int[] numbers = { 1, 2, 3, 4, 5 };

foreach (int number in numbers)
{
    Console.WriteLine(number);
}

Lists:

List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");

foreach (string name in names)
{
    Console.WriteLine(name);
}

9. Exception Handling

Use try-catch blocks to handle runtime errors gracefully.

try
{
    int number = int.Parse("ABC"); // This will throw an exception
}
catch (FormatException ex)
{
    Console.WriteLine("Input string was not in a correct format.");
}

10. Conclusion

This is a brief introduction to C# basics. From here, you can explore more advanced topics like:

  • Object-Oriented Programming (OOP) concepts like inheritance and polymorphism
  • LINQ (Language Integrated Query) for querying collections
  • Working with databases using Entity Framework Core
  • Building web apps using ASP.NET Core

For more detailed learning, check out official C# documentation and tutorials from Microsoft.

Happy coding!