Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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();
        }
    }

}
Posted
Updated 17-Jul-10 5:55am
v4
Comments
Nish Nishant 17-Jul-10 7:45am    
Reason for my vote of 5
Interesting thread, worth a 5.

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

Typos



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



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


The Bug


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

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.


Example without smoke and mirrors


C#
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
    }
}
 
Share this answer
 
v2
Comments
Nish Nishant 17-Jul-10 7:45am    
Reason for my vote of 5
Excellent answer. Worth 5. I propose this as an answer.
Sandeep Mewara 17-Jul-10 11:19am    
Reason for my vote of 5
Perfect! Nice explanation.
#realJSOP 17-Jul-10 11:56am    
Reason for my vote of 5
This *is* the answer. The bug is correctly stated.
boredombutcher 18-Jul-10 22:55pm    
I appreciate the feedback
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.
 
Share this answer
 
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):

VB
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


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

XML
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900