-1

my appliation crashes for some reason when i run this code, it just shuts down and tabs out of VS. For me everything seems correct but i am very new with C# and VS. It works properly when starting without debugging (CTRL + F5). Everything in the code seems correct to me, and i have no warnings within the code either.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace phoneSpace{

    class PhoneNumber{

        private string Number; 

        public string number{
             get; set; 
        }

        public PhoneNumber(string number){
             Number = number;
        }

        public void printNumber(){
            Console.WriteLine("{0}", Number);
        }

        static void Main(string[] args)
        {
            PhoneNumber phoneOne = new PhoneNumber("0703502341");
            phoneOne.printNumber();
        }
    }
}
hennamusick
  • 267
  • 1
  • 3
  • 17
  • Are you certain that it crashes? What's the exception? Try adding `Console.ReadLine()` at the bottom of your `Main()` method. – Cameron Jan 12 '17 at 17:19
  • 1
    Are you sure it shuts down? Maybe it just executes too quickly for you to see the results. VS will close the console window the moment the program finishes; this is normal. See [this question](http://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio). – John Wu Jan 12 '17 at 17:19
  • 1
    The code is not the issue, it works fine. – Equalsk Jan 12 '17 at 17:20
  • Note the dupe seems to be about C/C++ but the answer is the same. – TheLethalCoder Jan 12 '17 at 17:22
  • Your program is NOT crashing. This is a consequence of Visual Studio as indicated in @TheLethalCoder's duplicate link. By default when a program is done running it closes. If you go to your program's /bin folder and double click the .exe it will open, print your `Console.WriteLine` message, then close. This is normal. Visual Studio is *specially* launching it such that it stays open with the "press any key to continue". You can build similar behavior into your app like such: `Console.WriteLine("{0}", Number); Console.WriteLine("Press any key to continue.."); Console.ReadLine();`. – Quantic Jan 12 '17 at 17:23
  • I would expect you to get some form of warning regarding the _"string number"_ parameter to the constructor masking the _"public string number"_ property. It would be a good idea to modify the parameter name, to ensure the code is clear. – PaulF Jan 12 '17 at 17:28

3 Answers3

2

Are you sure it is crashing NOT auto closing after execution?

Code seems correct to me, just add the following line after last line inside main function.

Console.ReadKey();
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Shashi Bhushan
  • 1,292
  • 11
  • 15
0

Whenever you run the application in debug mode, and whenever the application finishes executing in the mode, it closes automatically, it doesn't happen in the non debug mode (run without debugging).

I suggest you should add Console.Read(); on the last line of the Main method, it waits for the user pressed key and then it closes.

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
0

It appears to me that the code is fine and that the application is just closing due to there being nothing for it to wait for. If you view this question (quoted for reference):

If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

So when running without the debugger Visual Studio will automatically prevent the application from closing.

To get around this when running with the debugger you have various options, such as setting a breakpoint or prompting the user for input:

Console.ReadKey(); //Any key press will close the application
//Or
Console.ReadLine(); //On ending a line the application will close: usually with enter
//etc

Also note the following code might produce a warning due to the variables being named the same as mentioned by @PaulF:

private string Number; 

public string number 
{
    get; set; 
}

public PhoneNumber(string number)
{
    Number = number;
}

I would change the code to match naming conventions and also make it so the property actually uses its backing field:

private string _number; 

public string Number { get { return _number; } set { _number = value; } }

public PhoneNumber(string number)
{
    _number = number;
}

And better yet seeing as the property isn't doing anything special I'd remove the field:

public string Number { get; set; }

public PhoneNumber(string number)
{
    Number = number;
}
Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69