This is part two of the C# Tutorial series. Before proceeding, I strongly
recommend you complete the first part.
In this article, we will continue to explore our first .NET application from Part One.
We are now going to delete all the unwanted lines of code, references, and files from the application which are auto-generated and are not required within our application and whose only task is to print "Hello World" in the command prompt.
Delete references
Go to View --> Solution Explorer.
In "HelloWorldProject", expand "References". Select all the references one by one, give a right click, select "Remove", and click "OK".
Delete the files
Go to View --> Solution Explorer.
In "HelloWorldProject", right click on "App.config", select "Delete", and click "OK".
Expand "Properties", right click on "AssemblyInfo.cs", select "Delete", and click "OK".
Now, your Solution Explorer will look like the one shown in the below image, after deleting all the unwanted references and files.
Delete the code
Hit Ctrl+A on "Program.cs", delete all the code, and paste the below code.
- class Program {
- static void Main(string[] args) {
- Console.WriteLine("Hello World");
- }
- }
As soon as you paste the code in "Program.cs", you will see a red squiggly line under the word "Console", as shown in the below image and if you hover your mouse on the red squiggly, it will show you a message
"The name 'Console' does not exist in the current context".
You have now deleted unwanted references, code, and files which are not required for your program execution.
As you learned in the first part, try to run the application by hitting "
Ctrl + F5" together (Alternatively, you can go to Debug, select "Start without Debugging"). You will immediately see the below notification that there are build errors.
Click "OK" and you will see the error "
The name 'Console' does not exist in the current context" listed in the "Error list" panel at the bottom.
Let us go back to "Program.cs" to correct the error. You see a red squiggly line under the word 'Console' which indicate a 'Design Time Error' and these are pointed out by Visual studio even before you run the program.
Before correcting the error, let us first try to understand the code present in "Program.cs".
My task is just to print a sequence of characters in the command prompt which can be achieved using the "
Console.WriteLine("Hello World");" statement and what's wrong in having just this one statement alone in "Program.cs" file ? Why should I have code blocks starting with '
class Program{}' and '
static void Main(string[] args){}' ?
This is because we are expected to follow the programming conditions set for Visual Studio for every line of code we write. Every line of code should be written inside a class and every application should have a 'static void Main(string[] args){}' block which is the entry point of the application.
For now, just remember 'Main(){}' is a 'method' which is the entry / starting point of the application and it is present in a 'Class'.
What's the error "The name 'Console' does not exist in the current context" ?
In simple words, Visual Studio is not able to understand the word 'Console'. You know that the 'Console.WriteLine()' statement prints the output in the command prompt, but your program doesn’t know what it is. So, it is the developer's responsibility to introduce any components that we use in the program using a 'Using' statement.
Before learning about 'Using' statement, let us discuss about 'namespace'. A namespace is a collection of classes which contains methods. So in our program, 'Console' is a class which contains a method 'WriteLine()' present in the namespace 'System'. To use a method present in a class, you will have to import the namespace in which the method's class is present. So, add the statement 'Using System;' above the class declaration, as shown in the below code sample to resolve the occurred error.
- using System;
- class Program {
- static void Main(string[] args) {
- Console.WriteLine("Hello World");
- }
- }
-
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) e[f];
- }
Now, run the application to see the below output.
Everything you learned about class, method, and namespace is just an assumption taken from the program written in this part and are not the actual definitions. All the new terminology introduced will be explained later.
In the next part, we will be discussing the below topics - What happens when we run the application?