是否有错误? [英] Is there a bug?

查看:73
本文介绍了是否有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,没有人看到此代码段中的任何错误.它不应该做任何有用的事情,但是在质量保证"课程中以代码为例向我展示了是否可以找出错误,并且似乎找不到任何错误.有人吗?

Hey does anyone see any bugs in this snippet. It isnt supposed to do anything useful but I was given the code as an example in a Quality Assurance course to see if I could pick out a bug and I cant seem to find anything wrong with it. Anyone?

using System;
namespace junk
{
    class Program
    {
        internal class a
        {
            internal a()
            {
                Console.WriteLine("seven and {0}", new b().whatAmI);
            }

            private class b : a
            {
                private string whatAmI = "one";
                internal string WhatAmI
                {
                    get
                    { 
                        Console.WriteLine(whatAmI);
                        return "nothing";
                    }
                }
            }
        }

        internal class c:a
        {
            internal c(string foo)
            {
                Console.Writeline("Four");
            }
        }

        static void Main(string[] args)
        {
           var test = new c("bar");
           Console.ReadKey();
        }
    }

}

推荐答案

存在三个问题-我认为其中两个是错别字.

错别字



Console.Writeline("Four");应该是Console.WriteLine("Four");

There are three issues - two of which are typos I think.

Typos



Console.Writeline("Four"); should be Console.WriteLine("Four");


new b().whatAmIwhatAmI字段是私有的,因此无法访问,我想您是想将该属性称为getter:
new b().WhatAmI


new b().whatAmI, the field whatAmI is private so inaccessible, I think you meant to call the property''s getter:
new b().WhatAmI

internal a()
{
    Console.WriteLine("seven and {0}", new b().WhatAmI);
}

由于b是从a派生的,因此对b的构造函数(new b())的调用实际上会调用a的构造函数,而该构造函数又会再次调用b的构造函数. ,其中...
结果是StackOverflowException.

As b is derrived from a, the call to b''s constructor (new b()) actually calls a''s constructor, which in turn calls b''s again, which ...
The result is a StackOverflowException.

class Program
{
    static void Main(string[] args)
    {
        new C();
    }

    public class A
    {
        // A's Constructor
        public A()
        {
            // Oops! This line creates a new B which will call this constructor again
            // due to inheritance and so an infinite loop is created.
            new B();
        }
    }
    public class B : A
    {
        // Creating a new B calls A's constructor
    }
    public class C : A
    {
        // Creating a new C calls A's constructor
    }
}


好吧,它似乎有一个属性,但是您已将其标记为C ++.是C#吗?在我看来就像这样.

这段代码看起来不会做任何有用的事情,除非您告诉我们您想要它做什么,它做什么,它们之间的区别以及您所做的事情,否则没有人会批评它.尝试修复它.
Well, it seems to have a property in it, but you''ve marked it as C++. Is it meant to be C# ? It looks like it to me.

This code doesn''t look like it would do anything useful, and no-one is going to critique it unless you tell us what you want it to do, what it does instead, and how they differ, and what you''ve done to try to fix it.


呵呵,我有一个与此类似的课程.
当他们给您这样的废话代码时,我讨厌它,而您应该找到该错误

以下是样式违规(Stylecop)的列表:

Hehe, I had a course simular to that.
I hated it when they give you crap code like that, and you''re suppose to find the bug

Here''s a list of style violations (Stylecop):

SA1633: The file has no header, the header Xml is invalid, or the header is not located at the top of the file. 1
SA1200: All using directives must be placed inside of the namespace.    1
SA1516: Adjacent elements must be separated by a blank line.    2
SA1300: namespace names begin with an upper-case letter: junk.  2
SA1600: The class must have a documentation header. 4
SA1400: The class must have an access modifier. 4
SA1600: The class must have a documentation header. 6
SA1300: class names begin with an upper-case letter: a. 6
SA1600: The constructor must have a documentation header.   8
SA1600: The class must have a documentation header. 13
SA1300: class names begin with an upper-case letter: b. 13
SA1600: The field must have a documentation header. 15
SA1600: The property must have a documentation header.  16
SA1516: Adjacent elements must be separated by a blank line.    16
SA1101: The call to whatAmI must begin with the 'this.' prefix to indicate that the item is a member of the class.  20
SA1513: Statements or elements wrapped in curly brackets must be followed by a blank line.  25
SA1600: The class must have a documentation header. 26
SA1516: Adjacent elements must be separated by a blank line.    26
SA1300: class names begin with an upper-case letter: c. 26
SA1600: The constructor must have a documentation header.   28
SA1600: The method must have a documentation header.    34
SA1400: The method must have an access modifier.    34
SA1201: All classes must be placed after all methods.   34
SA1508: A closing curly bracket must not be preceded by a blank line.   41



这是错误代码的列表(ReSharper代码分析器:



Here''s a list of bad code (ReSharper code analiser:

Common Practices and Code Improvements (6 issues)
   <ConsoleApplicationTest>\Program.cs (2 issues)
     Class 'b' has no inheritors and can be marked sealed
     'WhatAmI' Convert to constant
 Constraints Violations (2 issues)
   <ConsoleApplicationTest>\Program.cs (2 issues)
     Name 'a' does not match rule 'Types and namespaces'. Suggested name is 'A'.
     Name 'c' does not match rule 'Types and namespaces'. Suggested name is 'C'.
 Redundancies in Symbol Declarations (2 issues)
   <ConsoleApplicationTest>\Program.cs (2 issues)
     Parameter 'foo' is never used
     Parameter 'args' is never used
 Unused Symbols (1 issue)
   <ConsoleApplicationTest>\Program.cs (1 issue)
     Property 'WhatAmI' is never used
 Compiler Errors (2 issues)
   <ConsoleApplicationTest>\Program.cs (2 issues)
     Cannot access private field 'whatAmI' here
     Cannot resolve symbol 'Writeline'
 Compiler Warnings (2 issues)
   <ConsoleApplicationTest>\Program.cs (1 issue)
     Local variable 'test' is never used


这篇关于是否有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆