Before moving further let’s understand some code examples line by line.
Above source code explain line by line below
- The first line of the program using System; – the using keyword is used to include the System namespace in the program. As we print on the screen or to read the value from the screen we are using Console class and class is defined under System library hence to execute the above program we need to include system library in the program. A program generally has multiple using statements.
- The next line has the namespace declaration. A namespace is a collection of classes. The Applications namespace contains the class Program.
- The next line has a class declaration, the class Program contains the data and method definitions that your program uses. Classes generally contain multiple methods. You can read more about the class.
- The next line defines the Main method, which is the entry point for all C# programs. So when we execute the method program will start with this method.
- The next Line Print a message “Enter two numbers” on command prompt. (So when you want to display any message on command prompt you need used Console.WriteLine(“YOUR MESSAGE HERE”);)
- The next line will wait for the user to press some number on command prompt when the user enters some number and hit enter button our program converts the user input to number. And store the value into variable “x” (So when you want to accept any value from the command prompt you need used Console.ReadLine(), and store that value assign this value to a variable)
- The next line will wait for the user to press some number on command prompt when the user enters some number and hit enter button our program converts the user input to number. And store the value into variable “y” Note: by default, when the user enters some text it will be string format. So based on the program requirement we need to convert it similar way in the above example we have converted the string to int.
- The next line // is ignored by the compiler and it is put to add comments in the program.
- The next line we have defined one more variable “result” which holds the some of two variables value
- The next line to display result of addition of two numbers.
- The last line Console.ReadKey(); makes the program wait for a keypress and it prevents the screen from running and closing quickly when the program is executed.