C# Generic struct

Using generic struct


using System; //from  www .ja v a2s. c o m
 
struct GenStruct<T> { 
  T x; 
  T y; 
 
  public GenStruct(T a, T b) { 
    x = a; 
    y = b; 
  } 
 
  public T X { 
    get { return x; } 
    set { x = value; } 
  } 
 
  public T Y { 
    get { return y; } 
    set { y = value; } 
  } 
 
} 
 
class MainClass { 
  public static void Main() { 
    GenStruct<int> xy = new GenStruct<int>(10, 20); 
    GenStruct<double> xy2 = new GenStruct<double>(88.0, 99.0); 
 
    Console.WriteLine(xy.X + ", " + xy.Y); 
 
    Console.WriteLine(xy2.X + ", " + xy2.Y); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What are C# Interfaces
  2. How to define an interface
  3. Note for Interfaces
Home »
  C# Tutorial »
    C# Types »
      C# Struct
C# Structs
C# Struct Instances vs. Class Instances
C# struct Constructor
C# struct equality
C# Generic struct