Sold Out
Sold Out

Reputation: 1428

Where is Assert defined in C# .NET 3.5?

I have found tons of material how to use it, but not what using XXX.YYY.ZZZ; directive should I use. Not even on the MSDN pages...

I keep getting error:

"The name 'Assert' does not exist in the current context"

So what package should I declare I am using ?

Many thanks in advance !

Upvotes: 0

Views: 2774

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062745

Since there seems to be a lot of confusion over which Assert to use, and how, here's a fully working example (should compile fine, etc), using your C example from the comments of Assert(a !=b):

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        int a = 5, b = 10;
        Console.WriteLine("hello");
        Debug.Assert(a != b); // should get past this
        Console.WriteLine("world");
        b = 5;
        Debug.Assert(a != b); // should fail in debug mode
    }
}

Note this uses regular runtime assertions, not any particular / arbitrary test framework. Note that because Debug.Assert is a [Conditional("DEBUG")] method, it won't be invoked for release builds.

Upvotes: 1

MarkO
MarkO

Reputation: 2233

Assuming you're looking for Asserts in code, not unit tests:

System.Diagnostics.Debug.Assert

Upvotes: 2

Related Questions