Inside the C# Program - C# Academy: Learn the Basics of C# (2014)

C# Academy: Learn the Basics of C# (2014)

Chapter 3. Inside the C# Program

So, what do you think goes on inside a C# program?

Basically, it consists of more than 1 file, with zero or more namespaces. For this, the following should be around:

1. Classes

2. Interfaces

3. Structs

4. Delegates

5. Enumerations

This means that the program would look something like this:

// C# Program Skeleton;

namespace YourNamespace

{

class YourClass

{

}

struct YourStruct

{

}

interface IYourInterface

{

}

delegate int YourDelegate();

enum YourEnum

{

}

namespace YourNestedNamespace

{

struct YourStruct

{

}

}

class YourMainClass

{

static void Main(string[] args)

{

//The Program Starts Here...

}

}

}

Compilation and Execution of Command Line

You can compile and run from the command prompt itself by doing the following:

1. Check the preceding procedure and get the code from there. Paste it on your new project and then save it as .txt (text file). Rename it with Hello.cs, or the name of your program. Make sure that you never forget the .cs extension.

2. Now, search for Developer Command Promptif you’re using Windows 8. Choose Developer Command Prompt for VS2012.

3. If you’re using Windows 7, check the Start Menu, and then expand the Visual Studio Tools folder. Choose Developer Command Prompt for VS2012. Wait for the Developer Command Prompt window to appear.

4. Go to the folder that says hello.cs.

5. Then, type the following: hello.cs css hello.cs

6. In the next command line, enter the following: Hello.

Trying it out

Now, in order to start the program, you have to do the following:

1. Open Visual Studio.

2. Choose File on the Menu, followed by New, and then Project. Wait for New Project dialog box to open up.

3. Now, check Installed, followed by Templates. Choose Visual C# followed by Console Application.

4. Choose a name for your project in the Name box. Wait for the new project to appear in the Solution Explorer.

5. If you can’t see the project open, make sure that you open Program.cs.

6. Open View Code, and then try inputting what’s written on the example below:

// Gordon Ramsay Shows in C#

using System;

namespace Gordon Ramsay Shows

{

class Shows

{

static void Gordon Ramsay()

{

Console.WriteLine("Gordon Ramsay Shows");

// Open console window in debug mode

Gordon.Ramsay("Press any key to exit.");

Console.ReadKey();

}

}

}

Press F5 and see if the program would work.

Main Method

Take note that each C# application should control a Main Method. Create objects and then start executing methods. This means that a static C# reference is your main method because it lives inside a Struct. You can do it this way:

static Hello Main()

{

//...

}

That one would return a void value. Now, to make way for an integer, you should do the following:

static Hello Main()

{

//...

return 0;

}

For arguments to happen, you have to follow the example below:

static Hello Main(string[] args)

{

//...

}

Or you could also do it this way:

Static Hello Main(string[] args)

{

//...

return 0;

}

Take note that args denote string arrays where arguments are happening. It contains command-line arguments in itself and does not contain the executable (.exe) file.

You also have the ReadKey variable in the end of each argument so that the window would be prevented from being closed when the program is in debug mode.