locked
Class Inheritance new() RRS feed

  • Question

  • I have a quick question regarding the below code.  What is the purpose of the keyword new()?

    public class EntityBaseRepository<T> : IEntityBaseRepository<T>
            where T : class, IEntityBase, new()


    • Edited by zXSwordXz Thursday, August 24, 2017 7:44 PM
    Thursday, August 24, 2017 7:43 PM

Answers

    • Marked as answer by zXSwordXz Friday, August 25, 2017 12:37 PM
    Thursday, August 24, 2017 8:24 PM
  • Hello zXSwordXz,

    >>I have a quick question regarding the below code.  What is the purpose of the keyword new()?
    The statement where T:new()  has limited the generic T type must have the construction method without parameters . Some like below.

    class Program
        {
                EntityBaseRepository<GenricTest> e1 = new EntityBaseRepository<GenricTest>();    
        }
    
        class GenricTest : IEntityBase
        {
            public GenricTest()
            {
            }
    
     //public GenricTest(string s)
            //{
            //}
        }

    If the GenericTest type doesn't have no parameter construction method , the compiler will not let the code pass .

    Best regards,
    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 25, 2017 2:58 AM
    • Marked as answer by zXSwordXz Friday, August 25, 2017 12:37 PM
    Friday, August 25, 2017 2:57 AM

All replies

    • Marked as answer by zXSwordXz Friday, August 25, 2017 12:37 PM
    Thursday, August 24, 2017 8:24 PM
  • Hello zXSwordXz,

    >>I have a quick question regarding the below code.  What is the purpose of the keyword new()?
    The statement where T:new()  has limited the generic T type must have the construction method without parameters . Some like below.

    class Program
        {
                EntityBaseRepository<GenricTest> e1 = new EntityBaseRepository<GenricTest>();    
        }
    
        class GenricTest : IEntityBase
        {
            public GenricTest()
            {
            }
    
     //public GenricTest(string s)
            //{
            //}
        }

    If the GenericTest type doesn't have no parameter construction method , the compiler will not let the code pass .

    Best regards,
    feih_7


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Friday, August 25, 2017 2:58 AM
    • Marked as answer by zXSwordXz Friday, August 25, 2017 12:37 PM
    Friday, August 25, 2017 2:57 AM