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

  • Home
  • SEARCH
  • 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 3321810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:06:28+00:00 2026-05-17T23:06:28+00:00

Consider: class MyClass<T> where T : class { } In that case, the where

  • 0

Consider:

class MyClass<T> where T : class
{
}

In that case, the where clause is enforcing a specification that MyClass is only a generic of a reference type.

Ideally I should have a unit test that tests this specification. However, this unit test obviously won’t work, but it explains what I’m trying to accomplish:

[Test]
[DoesNotCompile()]
public void T_must_be_a_reference_type()
{
    var test = new MyClass<int>();
}

What can I do to test a spec that’s implemented by not allowing the code to compile?

EDIT:

More info: Ok, so my reasoning for doing this (haha) is that I’ve been following a TDD methodology, in which you can’t write any code unless you have a failing unit test. Let’s say you had this:

class MyClass<T> { }

What test can you write that would fail unless T were a class? Something like default(T) == null?

Further EDIT:

So after a “root cause analysis” on this, the problem is that I was relying on default(T) being null in a consumer of this class, in an implicit way. I was able to refactor that consumer code into another class, and specify a generic type restriction there (restricting it to class) which effectively makes that code not compile if someone were to remove the restriction on the class I’m talking about above.

  • 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-17T23:06:28+00:00Added an answer on May 17, 2026 at 11:06 pm

    Why would you need a unit test for this? Do you write a unit test for a method such as

    public void Foo(string x)
    

    to check that it can only take strings, and not integers? If not, what do you see as the difference?

    EDIT: Just to be slightly less whimsical: in this case the spec is validated by the declaration. Tests should generally test behaviour. That’s one of the things I like about Code Contracts: I don’t feel any need to unit test the contracts unless they express something complicated – in which case it’s that complexity that I’d be testing, not the “contracts are enforced” bit.

    EDIT: To respond to the question edit:

    What test can you write that would fail unless T were a class?

    You could write something like:

    Type definition = typeof(MyClass<>);
    Assert.Throws<ArgumentException>(() => definition.MakeGenericType(typeof(int)));
    

    However, that seems to be going against the real purpose of testing…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the class below that represents a Broker: public class Broker { public string
Consider this example (typical in OOP books): I have an Animal class, where each
Consider a hypothetical method of an object that does stuff for you: public class
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider these classes. class Base { ... }; class Derived : public Base {

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.