Unrecognized syntax in domain object constructor

Can anyone help me determine what is the purpose of this unrecognized syntax. It's a little overkill in the constructor for this object. I'm trying to figure out what is the "<IdT>" at the end of a class declaration line? I think this is what I find useful, I just need to understand why exactly so many people are doing this.

using BasicSample.Core.Utils;

namespace BasicSample.Core.Domain
{
    /// <summary>
    /// For a discussion of this object, see 
    /// http://devlicio.us/blogs/billy_mccafferty/archive/2007/04/25/using-equals-gethashcode-effectively.aspx
    /// </summary>
    public abstract class DomainObject<IdT>
    {
        /// <summary>
        /// ID may be of type string, int, custom type, etc.
        /// Setter is protected to allow unit tests to set this property via reflection and to allow 
        /// domain objects more flexibility in setting this for those objects with assigned IDs.
        /// </summary>
        public IdT ID {
            get { return id; }
            protected set { id = value; }
        }

      

0


source to share


4 answers


Read about Generics: MSDN

This is how you define a generic class. Hence the call

new DomainObject<string>(); 

      



will create a domain object with a type Id string.

The way you define the id should be int.

The way the identifier is determined can be any type you want.

+2


source


From the C # spec:



The type of the class that the type parameters are declared are called the generic class type. The structure, interface and types of delegates can also be shared. When using a generic class, enter arguments to be supplied for each of the type parameters

+1


source


In this case, it allows the ID property to take on different values ​​without having to define a class for each type of ID property. For example, the identifier can be int

either long

or Guid

depending on the underlying data type in the database.

+1


source


right I know a little about what generics are, used lists, delegates, etc. What I don't understand, I think, is what the effect is putting "<dataType>" after the class declaration, and why you would do that. If I were to declare this object, I would do the following:

Public class DomainObject {Public DomainObject (int ID) {this.ID = ID; } ....

0


source







All Articles