Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to use generic method with generic constructor in C#

1 Answer

0 votes
using System;
 
class Generic<T>
{
  private T gVariable;
 
  public Generic(T gValue) {
    this.gVariable = gValue;
  }
 
  public void Print() {
    Console.WriteLine(this.gVariable);
  }
}
 
class Program
{
  static void Main(string[] args)
  {
    Generic<int> g1 = new Generic<int>(12);
    Generic<string> g2 = new Generic<string>("C# Programming");
    Generic<float> g3 = new Generic<float>(3.14f);
    Generic<double> g4 = new Generic<double>(345.8916);
 
    g1.Print();
    g2.Print();    
    g3.Print();
    g4.Print();
  }
}
 
 
 
/*
run:
 
12
C# Programming
3.14
345.8916
 
*/

 



answered Dec 19, 2020 by avibootz

Related questions

1 answer 111 views
111 views asked Dec 19, 2020 by avibootz
1 answer 122 views
122 views asked Apr 19, 2020 by avibootz
1 answer 158 views
1 answer 148 views
1 answer 144 views
1 answer 137 views
137 views asked Dec 19, 2020 by avibootz
...