Click here to Skip to main content
15,845,829 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Genericclasses
{
    class Program
    {
        static void Main(string[] args)
        {
            somegeneric<forbidden> b = new somegeneric<forbidden>();
            somegeneric<forbidden> c = new somegeneric<forbidden>();
            b.a.value = 10;
            Console.ReadLine();
        }
    }
    class forbidden
    {
        public int value;
       

    }
    class somegeneric<T> where T:forbidden
    {
        public T a;
      
    }
    class somegeneric2<T> : somegeneric<T> where T:forbidden
    {
    }
   
}
what's wrong with this code.I get mistake "Object reference not set to an instance of an object" during debugging.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jan-15 20:36pm    
...and it has nothing to do with generics.
—SA
Sergey Alexandrovich Kryukov 5-Jan-15 23:51pm    
...and no, you did not ask any question on generic types and their constraints. If you will, you will certainly get some answers.
—SA
Inimicos 6-Jan-15 6:11am    
It is about generic classes.Becouse i can't understand how to implement constraint forbidden class in this code,without error it was my question.
Sergey Alexandrovich Kryukov 6-Jan-15 10:40am    
The exception was unrelated to generics, that's all. So, I guess, you don't have a confusion about implementing generics with the constraints. If you do, we will gladly help.
—SA

1 solution

The field a is never set.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jan-15 20:37pm    
Sure, a 5.
—SA
Inimicos 6-Jan-15 6:41am    
Ok how to set it?
Inimicos 6-Jan-15 7:33am    
I figured out thank's.I thought you mean create some property.get{} set{}.I should have simply initialize it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900