C# Tutorial Part 2 - The First Error In Your Application

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.

c-sharp

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.

c-sharp

Delete the code

Hit Ctrl+A on "Program.cs", delete all the code, and paste the below code.

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Console.WriteLine("Hello World");  
  4.     }  
  5. }  
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".

c-sharp

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.

c-sharp

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.

c-sharp

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.
  1. using System;  
  2. class Program {  
  3.     static void Main(string[] args) {  
  4.         Console.WriteLine("Hello World");  
  5.     }  
  6. }  
  7. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  8. if ('this_is' == /an_example/) {  
  9.     of_beautifier();  
  10. else {  
  11.     var a = b ? (c % d)  e[f];  
  12. }  
Now, run the application to see the below output.

c-sharp

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?