C# Tutorial
Main method return type is also one of the important thing in C# programming.
Main can also return a value if it is declared as int type instead of void.
If we use the return type as int in Main method, need to add return statement at the end of the method.
/* This C# program Main returning a value. Also added single and multiline comments. */ using System; // System is a namespace public class FirstApp { // Main method which starts the program execution. public static int Main() { // Prints welcome text Console.WriteLine("Welcome to codingpointer.com to learn C#."); return 0; // return statement } }
Output:
C:> csc FirstApp.cs C:> FirstApp Welcome to codingpointer.com to learn C#.
This program returns integer type value to the system and returned value serves as the program termination status code.
Purpose of this return value is to allow communication of success or failure to the execution environment.
C# program can have multiple calsses and Main method must be defined in any one of the class.
/* This C# program Using System namespace Also added single and multiline comments. */ using System; // System is a namespace public class Test { public void display() { // Prints welcome text Console.WriteLine("Welcome to codingpointer.com to learn C#."); } } public class FirstApp { // Main method which starts the program execution. public static void Main() { Test test1 = new Test(); test1.display(); Console.ReadKey(); } }
Output:
C:> csc FirstApp.cs C:> FirstApp Welcome to codingpointer.com to learn C#.
This program have two classes Test and First App, Main method is defined in FirstApp class and invokes display method to print text using instance of Test class.
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page