Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:03:16+00:00 2026-05-29T06:03:16+00:00

I have written a generic class class MyClass : MyClass<string> { } //this line

  • 0

I have written a generic class

 class MyClass : MyClass<string> { } //this line is in error

 class MyClass<OutputBaseType> : MyClass<OutputBaseType, List<OutputBaseType>>
     where OutputBaseType : new() //this new constraint is needed
 {}
 class MyClass<OutputBaseType,OutputType> 
    where OutputType : ICollection<OutputBaseType>, new()
    where OutputBaseType : new()
 {
     public void Method(OutputBaseType z)
     {
         if (z!=null) { ... }
     }
 ...
 }

Because string has no default constructor the first line will now work. If I remove this line and modify clients to explicitly define string, then it does not work for the same reason.

If I remove where OutputBaseType : new() and the other new() then it works with OutputBaseType of string but not with OutputType of dictionary<...>. This is because KeyValuePair<> used by dictionary is a struct not a class, so can not be null. (I don’t like null usually but need it here)

Is there a way to get this to work?


Amendments:

I have removed new constraint on OutputBaseType.
Then replaced assigning of null and assigning with default construction, with default(OutputBaseType)

This looks promising but I now have an error in the condition expression on line

OutputBaseType outputItem = ...;
if (outputItem != default(OutputBaseType)) { _Output.Add(outputItem); }

The error is:

Operator '!=' cannot be applied to operands of type 'OutputBaseType' and 'OutputBaseType'   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:03:16+00:00Added an answer on May 29, 2026 at 6:03 am

    If I remove where OutputBaseType : new() and the other new() then it works with string OutputBaseType but not with dictionary as OutputType. This is because KeyValuePair<> used by dictionary is a struct not a class, so can not be null. (I don’t like null usually but need it here)

    You can simply use default(T) instead of null for value types. The default constructor of value types cannot be user defined, and always initializes all fields to their default value (0, null,…). So you don’t need the new() constraint if you just use default(T) instead of new T(), since for value types they are equivalent, and for reference types you want null which happens to be the default of any reference type.


    == is not available on generic types. You can either use object.Equals (this causes boxing) or EqualityComparer<T>.Default.Equals. I prefer the second one:

    EqualityComparer<T>.Default.Equals(outputItem, default(OutputBaseType))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a list containing a custom class, MyClass MyClass has properties, which
I am very new to C# and i have written the following class: using
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using
I have a function like this defined in one class: using System; using System.Collections.Generic;
I have written a list() method for retrieving a list of domain class instances
Obviously I'm new to this, hence the content of this project. I have written
Suppose I have written a decorator that does something very generic. For example, it
I have a utility class library I am writing. A have written a factory
Background: I have written a generic logging library in .NET 3.5. Basically the developer
I have the following class which is intended to return the subject line of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.