निम्नलिखित कोड में, मैं एक रचनाकार में एक संरचना पास करता हूं जो कक्षा की अपेक्षा कर रहा है। यह संकलन और त्रुटि के बिना क्यों चलाता है (और वांछित आउटपुट का उत्पादन)?"जहां टी: कक्षा" संकलित समय या रन समय पर किसी भी तरह से लागू नहीं है?
class Program
{
static void Main()
{
var entity = new Foo { Id = 3 };
var t = new Test<IEntity>(entity); // why doesn't this fail?
Console.WriteLine(t.Entity.Id.ToString());
Console.ReadKey();
}
}
public class Test<TEntity> where TEntity : class
{
public TEntity Entity { get; set; }
public Test(TEntity entity)
{
Entity = entity;
}
public void ClearEntity()
{
Entity = null;
}
}
public struct Foo : IEntity
{
public int Id { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
मैं अपने Main()
पद्धति को बदलने इतना है कि यह ClearEntity()
के लिए एक कॉल भी शामिल है, जिन्हें आप नीचे है, यह अभी भी कोई त्रुटि उत्पन्न करता है। क्यूं कर?
static void Main()
{
var entity = new Foo { Id = 3 };
var t = new Test<IEntity>(entity);
Console.WriteLine(t.Entity.Id.ToString());
t.ClearEntity(); // why doesn't this fail?
Console.ReadKey();
}
'फू' 'आईएनएनटीटी 'होने के कारण, इसे बॉक्स किया जाएगा। बॉक्सिंग उदाहरण एक वर्ग का एक उदाहरण है। – Humberto
'एंटीटी' को 'TENTITY'' के रूप में परिभाषित किया गया है, जहां 'TENTITY: क्लास ', जो' आईएनएनटीटीआई 'लागू नहीं करता है, वह' t.Entity.Id' को कैसे संकलित कर सकता है? –