Ads

C# Programming 27 - Unveiling the Magic of Generics in C#


Hello Friends,

God Morning, Afternoon, Evening what ever zone you are in, In this article we will understanding the concept of Generics.



Generics in C# might sound like a complex topic, but fear not! In this article, we'll unravel the magic of generics in simple terms. Generics are a powerful feature that allows you to write flexible and reusable code while keeping things straightforward. Let's dive into the world of generics with a real-world example.

Understanding Generics:

Generics are like a superhero for your code. They enable you to write functions and classes that can work with different data types without sacrificing simplicity. Instead of creating separate versions of your code for each type, you write it once and let generics handle the rest.

The Problem:

Imagine you're writing a function to swap the values of two variables. Simple, right? But what if you want to swap not only integers but also strings, doubles, or any other data type? Writing a separate function for each type becomes impractical and repetitive.

Enter Generics:

Generics come to the rescue by allowing you to write a single function or class that works with various data types. Let's look at a simple example of a generic method to swap values:

using System;

public class Swapper
{
    public T SwapValues<T>(T a, T b)
    {
        Console.WriteLine($"Before swapping: a = {a}, b = {b}");

        T temp = a;
        a = b;
        b = temp;

        Console.WriteLine($"After swapping: a = {a}, b = {b}");

        return a;
    }
}

class Program
{
    static void Main()
    {
        // Create an instance of the Swapper class
        Swapper swapper = new Swapper();

        // Example with integers
        int num1 = 5, num2 = 10;
        Console.WriteLine("Swapping integers:");
        int resultInt = swapper.SwapValues(num1, num2);
        Console.WriteLine($"Result: {resultInt}\n");

        // Example with strings
        string str1 = "Hello", str2 = "World";
        Console.WriteLine("Swapping strings:");
        string resultString = swapper.SwapValues(str1, str2);
        Console.WriteLine($"Result: {resultString}\n");

        // Example with custom objects
        Person person1 = new Person { Name = "Alice", Age = 25 };
        Person person2 = new Person { Name = "Bob", Age = 30 };
        Console.WriteLine("Swapping custom objects:");
        Person resultPerson = swapper.SwapValues(person1, person2);
        Console.WriteLine($"Result: {resultPerson.Name}, {resultPerson.Age}\n");
    }
}

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

In this example, T is a placeholder for the actual data type.

The Swapper class has a generic method SwapValues<T> that swaps the values of two variables of the same type T.

The Main method in the Program class demonstrates how to use the Swapper class with different data types: integers, strings, and a custom Person object.

The Person class is a simple class with properties for a person's name and age, showcasing how the SwapValues method can work with custom objects.

When you run the program, you'll see the values before and after swapping for each data type. This illustrates how the generic SwapValues method adapts to various types seamlessly.

Finally!

Generics in C# are like a universal tool for writing flexible and reusable code. They simplify your life by allowing you to write logic that can adapt to different data types effortlessly. Whether you're swapping values, sorting boxes, or tackling any other coding challenge, generics are your go-to solution for simplicity and versatility in C#. Happy coding!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !