How to use generic class with two different types in C#

1 Answer

0 votes
using System;

class Generic<T, U>
{
  public T gVariableA {
    get;
    set;
  }

  public U gVariableB {
    get;
    set;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Generic<int, string> g1 = new Generic<int, string>();

    g1.gVariableA = 12;
    g1.gVariableB = "C# Programming";

    Console.WriteLine(g1.gVariableA);
    Console.WriteLine(g1.gVariableB);
    
    
    Generic<string, float> g2 = new Generic<string, float>();

    g2.gVariableA = "c# java c++";
    g2.gVariableB = 3.14f;

    Console.WriteLine(g2.gVariableA);
    Console.WriteLine(g2.gVariableB);
  }
}




/*
run:

12
C# Programming
c# java c++
3.14

*/

 



answered Dec 19, 2020 by avibootz

Related questions

1 answer 242 views
1 answer 236 views
1 answer 191 views
1 answer 171 views
1 answer 148 views
148 views asked Dec 19, 2020 by avibootz
...