Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#代码中如何以及在哪里使用断言?_C# - Fatal编程技术网

在c#代码中如何以及在哪里使用断言?

在c#代码中如何以及在哪里使用断言?,c#,C#,我需要知道在c#中如何使用断言以及在何处使用断言的示例 编辑我需要一些实际例子,说明在什么地方最适合使用断言。这就是为什么我需要那些使用过它们的人的例子,我能想到的直接答案是当你测试代码时: Assert.IsNotNull(yourVariable); 您还可以使用Debug.Assert System.Diagnostics.Debug.Assert(myBooleanProperty); 谷歌应该给你很多例子(提示:输入csharp-example-assert) 我更喜欢使用单元测试

我需要知道在c#中如何使用断言以及在何处使用断言的示例


编辑我需要一些实际例子,说明在什么地方最适合使用断言。这就是为什么我需要那些使用过它们的人的例子,我能想到的直接答案是当你测试代码时:

Assert.IsNotNull(yourVariable);
您还可以使用Debug.Assert

System.Diagnostics.Debug.Assert(myBooleanProperty);

谷歌应该给你很多例子(提示:输入csharp-example-assert)


我更喜欢使用单元测试来充实问题。因为我的主要产品是一个库/程序集,所以还需要确保参数经过验证并引发适当的异常


有趣的提示:Debug.Assert()被MS C编译器捕获在一个发布版本中并被删除。

不久前,关于Debug.Assert()的用法进行了很好的讨论:

当然是在单元测试中

在单元测试之外,我可以识别3种类型的错误(非常简单):

  • 严重内部错误:来自代码单元内部,用户无法影响的错误
  • 滥用某些功能:例如,给出null而不是某个值
  • 外部问题:例如不存在文件、没有网络连接等

  • 我认为断言应该只适用于第一个。第二个和第三个应该使用适当的异常来处理。

    我非常喜欢在调试期间使用Debug.Assert()甚至Debug.Fail()来支持异常处理:

    if(!无论我期望什么)
    {
    var message=“”;
    调试失败(消息);
    抛出新的无效操作(消息);
    }
    
    这样我就得到了非常方便的Debug.Assert()对话框,可以在忽略、重试和中止之间进行选择。“重试”将进入可以开始调试的代码。
    特别是递归方法或复杂的try-catch构造可以通过这种方式进行更好的调试。您总是得到正确的调用堆栈。在我的发布代码中,我仍然有一个没有MessageBox的有效错误处理。

    我经常使用Debug.Assert检查代码的前提条件。如果一个对象依赖于存在的另一个对象,我可以断言这个另一个对象不是空的。这使得很多问题甚至在我把程序交给测试之前就已经显现出来了

    在我想用单元测试检查的内容和我想在运行时用断言检查的内容之间有一条细线。通常,单元测试用于测试单个代码单元,当应用程序的其余部分处于一个良好定义的状态时(我广泛使用模拟对象),并且如果应用程序的状态与我预期的一样,则在运行时使用Debug .Asvt进行检查。

    < P>有不同的视图(参见COMP.Lang.c++中的最近子线程)。“我们不使用C++异常”(

    ) 我认为断言最好是检查和记录你知道不可能是错误的东西,除非实现不一致。这比前置条件和不变量更强

    例如,一个只能从几个不同位置调用的helper函数有一个可用的资源。因为这个helper是私有的,并且只在一个类中使用,所以抛出ArgumentException是不合适的(这适用于公共/受保护的接口)。

    我使用了Debug.Assert()在代码中,有相当一部分永远不会收到错误/无效的参数,这种代码的输入参数和依赖变量应该已经过验证。事实是,我知道我会犯错误。我只是不知道在哪里。:-“Debug.Assert”的司法用法帮助我处理我的缺陷

    下面是我自己代码中的一个示例:

        public bool AddPoint(uint time, object value)
        {
            bool success = false;
            int index = 0;
    
            Debug.Assert(_selectedParameterName != null, "RecipeBusinessObject.InsertPointAtTime() _selectedParameterName = null");
            Debug.Assert(_currentRecipe != null, "RecipeBusinessObject.InsertPointAtTime() _currentRecipe = null");          
            Debug.Assert(CurrentParameterTable != null, "RecipeBusinessObject.InsertPointAtTime() CurrentParameterTable = null");
    
            Debug.Assert(index >= 0, "RecipeBusinessObject.InsertPoint(), index out of range, index = " + index.ToString());
            Debug.Assert(index <= CurrentParameterTable.Rows.Count, "RecipeBusinessObject.InsertPoint(), index out of range, index = " + index.ToString());
    
            DataRow newRow = CurrentParameterTable.NewRow();
    
            <snip>
    
    public bool AddPoint(uint时间,对象值)
    {
    布尔成功=假;
    int指数=0;
    Assert(_selectedParameterName!=null,“RecipeBusinessObject.InsertPointAtTime()_selectedParameterName=null”);
    Assert(_currentRecipe!=null,“RecipeBusinessObject.InsertPointAtTime()_currentRecipe=null”);
    Assert(CurrentParameterTable!=null,“RecipeBusinessObject.InsertPointAtTime()CurrentParameterTable=null”);
    Assert(index>=0,“RecipeBusinessObject.InsertPoint(),索引超出范围,index=“+index.ToString());
    
    Assert(index)你能详细解释一下为什么你需要知道这个吗?这将有助于解释示例。
        public bool AddPoint(uint time, object value)
        {
            bool success = false;
            int index = 0;
    
            Debug.Assert(_selectedParameterName != null, "RecipeBusinessObject.InsertPointAtTime() _selectedParameterName = null");
            Debug.Assert(_currentRecipe != null, "RecipeBusinessObject.InsertPointAtTime() _currentRecipe = null");          
            Debug.Assert(CurrentParameterTable != null, "RecipeBusinessObject.InsertPointAtTime() CurrentParameterTable = null");
    
            Debug.Assert(index >= 0, "RecipeBusinessObject.InsertPoint(), index out of range, index = " + index.ToString());
            Debug.Assert(index <= CurrentParameterTable.Rows.Count, "RecipeBusinessObject.InsertPoint(), index out of range, index = " + index.ToString());
    
            DataRow newRow = CurrentParameterTable.NewRow();
    
            <snip>