AnswerBun.com

Understanding Generic Class implementation

Stack Overflow Asked by Yeo on February 9, 2021

I am trying to understand this code:

public interface IBuilding
{
    string Id { get; }
}

public class Model<TBuilding>
    where TBuilding : IBuilding
{
    public TBuilding Building { get; private set; }
}

What does Model<TBuilding> mean here? From my understanding, ClassName<T> stands for Generic Class. <T> can represent string, int etc. But <TBuilding> implements IBuilding interface here. Why does a generic type implement an interface? My suspicion is that <T> means <Id>. Am I correct?

I have no clue on how to understand this code. I read up on Generic Classes but could not find anything useful.

3 Answers

This is a little misleading by naming

public interface IBuilding // interface contract
{
    string Id { get; }
    string ElevatorCount { get; }
}

// this is where you have confusion. T - is standard naming for generic type parameter
// if you start naming your generic type parameter TBuilding - 
// you will get confused that it might be some type
public class BuildingInspector<T>  where T : IBuilding // T : IBuilding - restricts types to IBuilding
{
    public BuildingInspector(T building) // generic type in constructor
    {
        Building = building;
    }

    public T Building { get; private set; }
    public int GetTotalElevatorCapacity(int kiloPerElevator)
    {
        return (this.Building.ElevatorCount * kiloPerElevator)  
    }
}

// U S A G E

public class SingleFamilyHome : IBuilding
{
    public string Id { get; private set; }
    public string ElevatorCount { get; private set; }
}
 . . . . .
private void TryUsingGenericType ()
{

    var famHome = new SingleFamilyHome(){ Id=1, ElevatorCount=0 };
    var famInspector = new BuildingInspector<SingleFamilyHome>(); // SUCCESS

    var capacity = famInspector.GetTotalElevatorCapacity(0);

    // this code will not compile because above you have --> where T : IBuilding
    var stringInspector = new BuildingInspector<string>(); // DESIGN TIME ERROR

}

Now that you see declaration AND usage, you can see how it is used. Best example is System.Collections.Generic.List<T>. You can have any list

List<string>
List<Building>
List<int>

Answered by T.S. on February 9, 2021

'Where' syntax is constraining your T type to be a type that implements IBuilding. Without the where condition, it would accept any types. Here you can find a more detailed explanation. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint

Answered by Bujor Iosif on February 9, 2021

Not exactly. T is just a placeholder for a type. If you have FireHouse, PoliceStation, Hospital, etc. implementing IBuilding you can provide one of those to Model and the Building property would only hold an instance of that type.

Answered by Daniel A. White on February 9, 2021

Add your own answers!

Related Questions

Trying to get property ‘post_title’ of non-object with

1  Asked on January 1, 2022 by german-mazzaferro

   

Select max for a tuple in table

2  Asked on January 1, 2022 by maxsteel

         

bind python/flask module app yo WINDOWS Server IIS

1  Asked on January 1, 2022 by shaul-sondak

     

Break apart email attachment if zip is too big to email

0  Asked on January 1, 2022 by programmingislife

       

Retrieve previous position of an Image on refresh page

2  Asked on January 1, 2022 by roel-thijssen

 

EMP id Masking with asterisk inbetween string

2  Asked on December 30, 2021 by bala

   

Create Sidebar Nav menu

1  Asked on December 30, 2021

     

Avoid select from dual Union in PL/SQL

3  Asked on December 30, 2021 by alc

     

SQL pulling distinct values and creating an ID

2  Asked on December 30, 2021 by jamheadart

     

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP