Giter VIP home page Giter VIP logo

csharp2_generics's Introduction

Csharp2_Generics

Generics in C# allow you to create classes, interfaces, and methods that can work with various data types without sacrificing type safety. They provide a way to define placeholders for types that are determined at compile time, enabling code reuse and ensuring type safety at the same time.

Here's an example to illustrate how generics work in C#:

// Example of a generic class
public class MyGenericClass<T>
{
    private T genericField;

    public MyGenericClass(T value)
    {
        genericField = value;
    }

    public T GenericMethod(T argument)
    {
        Console.WriteLine("Generic method called with argument: " + argument);
        return genericField;
    }
}

// Example of a generic method
public T GenericMethod<T>(T argument)
{
    Console.WriteLine("Generic method called with argument: " + argument);
    return argument;
}

In the above code, we have a generic class called MyGenericClass that has a type parameter T. The constructor and GenericMethod both use this type parameter. The MyGenericClass can be instantiated with any type, and the type safety is maintained because the actual type is determined when creating an instance of the class.

Here's an example of how you can use the generic class and method:

// Using the generic class
MyGenericClass<int> intInstance = new MyGenericClass<int>(42);
int result = intInstance.GenericMethod(10);
Console.WriteLine("Returned value: " + result);

MyGenericClass<string> stringInstance = new MyGenericClass<string>("Hello");
string result2 = stringInstance.GenericMethod("World");
Console.WriteLine("Returned value: " + result2);

// Using the generic method
int intResult = GenericMethod<int>(5);
Console.WriteLine("Returned value: " + intResult);

string stringResult = GenericMethod<string>("Hello");
Console.WriteLine("Returned value: " + stringResult);

In this example, we create instances of MyGenericClass with different type arguments (int and string) and call the GenericMethod with different arguments and return types. The generic method is also invoked with specific type arguments (int and string).

Generics in C# provide flexibility and code reuse by allowing you to write classes and methods that can work with multiple types, promoting type safety and reducing code duplication.

csharp2_generics's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.