0

I have been looking for definition for DeleteObject method.. But I found something which I don't know.

public class ObjectSet<TEntity> : ObjectQuery<TEntity>, IObjectSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable where TEntity : class

This is the signature of class defination of ObjectSet in the namespace System.Data.Objects , can anybody please explain me this defination.. I am more curious about where TEntity : class part.

Mayur A Muley
  • 51
  • 1
  • 12

2 Answers2

2
public class ObjectSet<TEntity> : ObjectQuery<TEntity>, ... where TEntity : class

The where TEntity : class is a Generic Type Constraint. In this case it means that TEntity has to be a reference type (and thus cannot be a value type like int or DateTime)

H H
  • 263,252
  • 30
  • 330
  • 514
1

The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

https://msdn.microsoft.com/en-us/library/d5x73970.aspx

Sometimes it is important to make this restriction because there are certain things that you can do with a reference type and cannot with a value type.

In this case (entity framework) it is logical that an entity must be a reference type. If a TEntity would be a struct then it could not track the changes (you would have several different 'copies' in different stacks).

p.s. If you don't understand the difference between value and reference types then this might help: https://stackoverflow.com/a/5057284/238682

Community
  • 1
  • 1
Peter Porfy
  • 8,921
  • 3
  • 31
  • 41
  • Any real case scenario where I should use constraints? – Mayur A Muley Jul 27 '15 at 09:15
  • see my edits. because ref and value types are behaving differently you need to differentiate them sometimes – Peter Porfy Jul 27 '15 at 09:16
  • `public class Teacher { public string name{get;set;} public Teacher(string s) { name=s; } } public class Student { public string name{get;set;} public Student(string s) { name=s; } }` Now `public class MyGenericList where T: Employee { private Teacher teacher1; public MyGenericList( T t) { teacher1=t; } }` This means, I can do `var goodTeacher= new Teacher("Peter"); var obj = new MyGenericList(goodTeacher);` and I can't do, `var goodStudent = new Student("Mayur"); var obj2 = new MyGenericList(goodStudent);` Did I correctly understood? – Mayur A Muley Jul 27 '15 at 09:43
  • To undestand this you first need to understand what a generic type is. A generic type is a kind of template. It is an uncompleted class. It needs additional information. In the question: ObjectSet TEntity is just a placeholder name. It could be TPeter for example. It just marks that an ObjectSet needs this additional type information called TEntity. Once you start using template classes (generic types) you will realize that sometimes you need to restrict them in some ways. – Peter Porfy Jul 27 '15 at 09:50
  • Restriction can vary from problem to problem. You might need a restriction so the type parameter (TEntity in this example) must inherit from a certain base class so it is guaranteed that a property will be there. For another type you might need a less restrictive rule such as the `class` keyword which allows every kind of class but not structs. This is the case for ObjectSet. It is required for ObjectSet to work. – Peter Porfy Jul 27 '15 at 09:53
  • Thanks @Peter for that explaination. – Mayur A Muley Jul 27 '15 at 10:07