0

When trying to create a generic class in C# I am doubtful about how to proceed.

Say I want to create a generic class with certain values on Blazor. (most of the time I've seen TItem used in Blazor projects, be as part of a component or in pure C# classes)

public class Disc<TItem> where TItem : class
{
    public IEnumerable<IValues<TItem>> Dataset { get; set; }
  [JsonIgnore]
    public string Title{ get; set; }
}

public interface IValues<TItem>
{
    IEnumerable<TItem> Items { get; set; }
    object Song { get; set; }
    string Lyrics { get; set; }
}

Isn't that the same as this? Just curious....

public class Disc<T> where T : class
{
    public IEnumerable<IData<T>> Dataset { get; set; }
  [JsonIgnore]
    public string Title { get; set; }
}

public interface IValues<T>
{
    IEnumerable<T> Items { get; set; }
    object Song { get; set; }
    string Lyrics { get; set; }
}
xestla
  • 1
  • 1

2 Answers2

0

Yes it is the same - the name is just how you identify the generic type in the source code. They should get compiled to the exact same byte code.

It's more helpful to use longer type names when you have multiple generic types and you want to indicate their purpose, like Dictionary<TKey, TValue>

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Yes, they are the same. When writing generics, you get to name the type placeholders anything you want, just like a variable or class name.

Different projects or organizations may follow different conventions. T is the most common for generics with a single type, and single-letter mnemonics are common for generics with multiple types, but you also sometimes see a T followed by the name, such as TItem.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794