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:
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;
    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.
Sergey Alexandrovich Kryukov 5-Jan-15 20:36pm    
...and it has nothing to do with generics.
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.
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.

1 solution

The field a is never set.
Share this answer
Sergey Alexandrovich Kryukov 5-Jan-15 20:37pm    
Sure, a 5.
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