Hey guys,

I'm hoping you could explain why this doesnt work:

main file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TemplateApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1<int> cl = new Class1<int>();
        }
    }
}

another template class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TemplateApplication
{
    class Class1<T>
    {
        T t;
  

        public T getValue()
        {
            return t;
        }
    }
}

Now why does it generate the following warning?

Warning 1 Field 'TemplateApplication.Class1<T>.t' is never assigned to, and will always have its default value C:\Users\John\Documents\Visual Studio 2008\Projects\TemplateApplication\TemplateApplication\Class1.cs 10 11 TemplateApplication

Recommended Answers

All 2 Replies

It generates the warning for the exact reason stated. You never assign a value to the member variable t. If you change the line T t; to T t = new T(); then it should work.

idiotic me! It needed a constructor. Man can't beleive i overlooked that.

Cheers for your help btw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.