C# -

Introduction

C# is a modern, object-oriented programming language developed by Microsoft. It is part of the .NET ecosystem and is used to build a wide variety of applications, from desktop to web to mobile. This guide provides an introduction to C# and covers its key features, syntax, and basic programming concepts.


1. Key Features of C#

C# is designed to be simple, modern, and versatile. Here are some of its key features:


2. Basic Syntax

The basic syntax of C# is similar to other C-style languages like C++, Java, and JavaScript. Below are some examples of basic syntax elements.

2.1 Hello World Program Example:
        
            using System;

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

2.2 Variables and Data Types Example:
        
            using System;

namespace VariablesAndDataTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 30;
            string name = "John";
            bool isStudent = false;
            double salary = 3000.50;

            Console.WriteLine($"Name: {name}, Age: {age}, Student: {isStudent}, Salary: {salary}");
        }
    }
}
        
    

2.3 Conditional Statements Example:
        
            using System;

namespace ConditionalStatements
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 20;

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

2.4 Loops Example:
        
            using System;

namespace Loops
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"For loop iteration: {i}");
            }

            int j = 0;
            while (j < 5)
            {
                Console.WriteLine($"While loop iteration: {j}");
                j++;
            }
        }
    }
}
        
    

3. Object-Oriented Programming (OOP) Concepts

C# is an object-oriented language, which means it uses objects to model real-world entities. Below are the four main principles of OOP.


3.1 Classes and Objects Example:
        
            using System;

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

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

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person
            {
                Name = "John",
                Age = 30
            };
            person.Introduce();
        }
    }
}
        
    

3.2 Inheritance Example:
        
            using System;

namespace Inheritance
{
    class Animal
    {
        public string Name { get; set; }

        public void Eat()
        {
            Console.WriteLine($"{Name} is eating.");
        }
    }

    class Dog : Animal
    {
        public void Bark()
        {
            Console.WriteLine($"{Name} is barking.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog
            {
                Name = "Buddy"
            };
            dog.Eat();
            dog.Bark();
        }
    }
}
        
    

3.3 Polymorphism Example:
        
            using System;

namespace Polymorphism
{
    class Animal
    {
        public virtual void MakeSound()
        {
            Console.WriteLine("Some generic animal sound.");
        }
    }

    class Dog : Animal
    {
        public override void MakeSound()
        {
            Console.WriteLine("Bark!");
        }
    }

    class Cat : Animal
    {
        public override void MakeSound()
        {
            Console.WriteLine("Meow!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal myDog = new Dog();
            Animal myCat = new Cat();

            myDog.MakeSound();
            myCat.MakeSound();
        }
    }
}
        
    

3.4 Abstraction Example:
        
            using System;

namespace Abstraction
{
    abstract class Shape
    {
        public abstract double GetArea();
    }

    class Circle : Shape
    {
        public double Radius { get; set; }

        public Circle(double radius)
        {
            Radius = radius;
        }

        public override double GetArea()
        {
            return Math.PI * Math.Pow(Radius, 2);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Shape circle = new Circle(5);
            Console.WriteLine($"The area of the circle is {circle.GetArea()}");
        }
    }
}
        
    

4. Advanced Features

C# offers many advanced features that make it a powerful language for building complex applications. Below are some of these features.

4.1 LINQ (Language Integrated Query) Example:
        
            using System;
using System.Linq;

namespace LINQExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            var evenNumbers = from number in numbers
                              where number % 2 == 0
                              select number;

            Console.WriteLine("Even numbers:");
            foreach (var number in evenNumbers)
            {
                Console.WriteLine(number);
            }
        }
    }
}
        
    

4.2 Asynchronous Programming Example:
        
            using System;
using System.Threading.Tasks;

namespace AsyncProgramming
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await FetchDataAsync();
            Console.WriteLine("Data fetched successfully.");
        }

        static async Task FetchDataAsync()
        {
            // Simulate an asynchronous operation
            await Task.Delay(2000);
            Console.WriteLine("Data fetching...");
        }
    }
}
        
    

4.3 Exception Handling Example:
        
            using System;

namespace ExceptionHandling
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int result = Divide(10, 0);
                Console.WriteLine($"Result: {result}");
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }

        static int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}
        
    

5. Best Practices


6. Conclusion

C# is a powerful and versatile programming language that is well-suited for building a wide range of applications. By understanding its key features, syntax, and object-oriented principles, you can start developing robust and maintainable applications. Follow best practices to ensure your code is clean, efficient, and easy to understand.