c# - Generic contraints -


i've got class want enforce generic constraints on when created

myobject.cs

    public class myobject<t> t : objectbase, new()     {         public myobject()         {         }          public bool validate(out serviceerror? message)         {             // validation         }     } 

objectbase.cs

public abstract class objectbase {     public abstract bool validate(out serviceerror? message); } 

i'm wondering if there way avoid having place generic constraints t every time have function such this:

objectrepo.cs

public void put<t>(myobject<t> obj)   t : objectbase, new() {     // code } 

if constraints t specified in myobject class, there reason have re-specify every single time use myobject parameter?

don't redefine t on method if have on class

public class myobject<t> t : objectbase, new() {     //public void put<t>(myobject<t> obj)//doesn't work     public void put(myobject<t> obj)//works     {         // code     } } 

update
if put in class best solution refactor code , move t related method in same class (and add constrain on class)

update 2
if update 1 not possible ,it might worth checking if can replace t objectbase or base class


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -