C# - Generic function to modify data structures specific to each type

C# - Generic function to modify data structures specific to each type

generics in c# with real time example
generics in c# codeproject
how generics are type-safe in c#
generic interface in c#
generic in c# geeksforgeeks
generics and collections in c#
c# generic constraints
c# struct

I am having some trouble with generics.

I want to write a generic function that given a type, modifies a data structure of that type.

public static Dictionary<int, User> Users = new Dictionary<int, User>();
public static Dictionary<int, Item> Items = new Dictionary<int, Item>();
public static Dictionary<int, Store> Stores = new Dictionary<int, Store>();

public static void AddStuff<T>(T stuffToAdd)
{
    (What dict goes here?).TryAdd(stuffToAdd.Id, stuffToAdd)
}

I want to call it like:

AddStuff<User>(some_user);

So depending on what type it's called with, I have to figure out how to find the data structure associated with that type and add/remove stuff from it. Any ideas for me?

Edit: I have 20 of these dictionaries.


I am having some trouble with generics.

Yes you are. Pay attention to that feeling. It is telling you that you are abusing generics.

I want to write a generic function that given a type, modifies a data structure of that type.

My best advice is: stop wanting that. Generics are supposed to be generic. That's why they're called generics. If you are doing different things based on the generic type argument then your code is not generic.

I want to call it like: AddStuff<User>(some_user);

Stop wanting that.

Instead, write three methods, and call them like:

AddUserStuff(someUser);
AddItemStuff(someItem);
AddStoreStuff(someStore);

and so on. You logically have three methods that do three different things, so they are not generic. Write three methods!

Edit: I have 20 of these dictionaries.

Write 20 methods. It's not hard, and it is easier than writing a "generic" method that has twenty cases and is then a bug farm because it looks generic but really is not.

Collections and Data Structures, Immutable namespaces to add, remove, and modify either individual elements or a There are two main types of collections; generic collections and All collections can be copied to an array using the CopyTo method;  You might also notice that the type for the generic list is distinct ([System.Int32]), whereas the type for the non-generic list is generalized. Because the runtime knows the generic List<int> is of type Int32 , it can store the list elements in an underlying integer array in memory, while the non-generic ArrayList has to cast each list element to an object.


Seeing your usage AddStuff<User>(some_user), which could also be AddStuff(some_user), I don't see the point of having a generic. Instead of using something that can't be compile-time validated (is the generic User, Item or Store or something else). For instance, AddStuff<string>("oops invalid usage") would give a runtime issue. You might as well use overloads for each type. You can have a private helper function such as Paul's extension example.

Using something like John Wu's idea of using IHasId interface, you can create a private helper like this:

private static void AddStuff<T>(Dictionary<int, T> dict, T stuffToAdd)
    where TValue  : IHasId
{
    dict.TryAdd(stuffToAdd.Id, stuffToAdd)
}

You can use it and create simple overloads:

public static void AddStuff(User user) => AddStuff(Users, user);
public static void AddStuff(Item item) => AddStuff(Items, item);
public static void AddStuff(Store store) => AddStuff(Stores, store);

Classes and Structs, Feedback; Edit Each is essentially a data structure that encapsulates a set of data and behaviors If you declare and initialize a variable p of type Person , p is said to be an The Main method in the Program class creates an instance (​object) of Generic namespace is defined with one type parameter. C# - Generics. Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.


You can use a if/else if or switch (C#7 up only) to accomplish this.

public static void AddStuff<T>(T stuffToAdd)
{
    switch (stuffToAdd)
    case User:
        Users.TryAdd(stuffToAdd.Id, stuffToAdd);
    // More cases
}

P.S.: on mobile and on bus. So please make necessary corrections to make it compile etc. Also as others pointed out, you don't actually need generic here. So don't use what I've shown here!

Generics, Feedback; Edit For example, by using a generic type parameter T , you can write a single class As the type of a method parameter in the AddHead method. As the return type of the Data property in the nested Node class. a concrete type, for example as a GenericList<int> , each occurrence of T will be  Generics in C# is its most powerful feature. It allows you to define the type-safe data structures. This out-turn in a remarkable performance boost and high-grade code, because it helps to reuse data processing algorithms without replicating type-specific code.


To be able to access the Id property, you need a common interface and a type constraint. In my example I include an IHasId interface so all domain types have that ID available.

To pick the dictionary, you just need to arrange them in a bigger dictionary. In my example I call this dictionaries.

//This interface allows you to specify a type constraint so AddStuff can use the Id property
public interface IHasId
{
    int Id { get; }
}

public class User  : IHasId { public int Id { get; set; } }
public class Item  : IHasId { public int Id { get; set; } }
public class Store : IHasId { public int Id { get; set; } }

public class Program
{
    public static Dictionary<int, User> Users = new Dictionary<int, User>();
    public static Dictionary<int, Item> Items = new Dictionary<int, Item>();
    public static Dictionary<int, Store> Stores = new Dictionary<int, Store>();

    //This "outer" dictionary will allow AddStuff to look up the right dictionary based on the type.
    public static Dictionary<Type,IDictionary> dictionaries = new Dictionary<Type,IDictionary>
    {
        { typeof(User ), Users },
        { typeof(Item ), Items }, 
        { typeof(Store), Stores}
    };

    public static void AddStuff<TValue>(TValue stuffToAdd) where TValue  : IHasId
    {
        IDictionary d = dictionaries[typeof(TValue)] as Dictionary<int, TValue>;
        d.Add(stuffToAdd.Id, stuffToAdd);
    }

    public static void Main()
    {
        AddStuff( new User  { Id = 1} );
        AddStuff( new User  { Id = 2} );
        AddStuff( new Item  { Id = 3} );
        AddStuff( new Item  { Id = 4} );
        AddStuff( new Store { Id = 5} );
        AddStuff( new Store { Id = 6} );

        Console.WriteLine("Users:  " + string.Join(",", Users.Select( u => u.Value.Id )));
        Console.WriteLine("Items:  " + string.Join(",", Items.Select( i => i.Value.Id )));
        Console.WriteLine("Stores: " + string.Join(",", Stores.Select( s => s.Value.Id )));
    }
}

Output:

Users:  1,2
Items:  3,4
Stores: 5,6

Link to DotNetFiddle example that works

Using Generics in C# to Improve Application Maintainability , In a previous article, Data and Encapsulation in complex C# applications, how we can use generics in C# to make it easier to handle data-related change requests in changing behavior with little or no changes to data structure/​representation. classes to do the things that are specific to each data type. General Trees is the most generic kind of tree data structure. It allows for any number of child nodes for a given node (including zero nodes, which makes it a leaf node). The GeneralTree<T> class, like the BinaryTree<T> class, allows for breadth-first traversal and depth-first traversal for Visitors.


C#, It allows you to define the type-safe data structures. Parameter types are specified in generic class creation. To create objects of generic class, following syntax is used: BaseType obj = new BaseType (). Example: filter_none. edit close A Generic method with various parameters: Just as a method can take one argument,  The foreach, in statement and the For EachNext Statement use the enumerator exposed by the GetEnumerator method and hide the complexity of manipulating the enumerator. In addition, any collection that implements System.Collections.Generic.IEnumerable<T> is considered a queryable type and can be queried with LINQ. LINQ queries provide a common pattern for accessing data.


S3 · Advanced R., A method is a function associated with a particular type of object. This is different to most programming languages, like Java, C++ and C#, These methods allow us to have one generic function, e.g. print() , that Class is stored as an attribute, but it's better to modify it using the class() function, since this communicates  Generics are the most powerful feature of C# . Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code.


Programming C# 5.0: Building Windows 8, Web, and Desktop , this for you for one-dimensional arrays, with its Resize method. data around within arrays are useful for building more flexible data structures on top List<T> implement various common generic collection interfaces that we'll be With immutable value types, this distinction is moot, because you cannot modify their values  Design the data structures for a generic deck of cards Explain how you would sub-class it to implement particular card games and how you would subclass the data structures to implement blackjack. First, we need to recognize that a “generic” deck of cards can mean many things. Generic could mean a standard deck of cards that can play a poker