#535 – Creating a Generic Struct

In addition to generic classes, you can also create a generic struct.  Like a class, the generic struct definition serves as a sort of template for a strongly-typed struct.  When you declare a variable of this struct type, you provide a type for its generic parameter.

    public struct ThreeTuple<T>
        public ThreeTuple(T x, T y, T z)
            X = x;
            Y = y;
            Z = z;

        public T X;
        public T Y;
        public T Z;

    public class Program
        static void Main()
            ThreeTuple<int> intTuple = new ThreeTuple<int>(32, 10, 12);
            int yVal = intTuple.Y;

            ThreeTuple<double> dblTuple = new ThreeTuple<double>(1.2, 3.4, 5.6);
            double yVal2 = dblTuple.Y;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: