C# Tutorial
In C# program, we can define Main method in more than one classes but during compilation need to specify which class Main method needs to be used.
Need to specify which Main method needs to start the execution
csc filename.cs /main:class-name
This is the sample C# program which has more than one classes define Main method.
using System; // System is a namespace /* This C# program to calculate square root value of number. Also added single and multiline comments. */ public class SqrtCalc { // Main method which starts the program execution. public static void Main() { // Declaration and initialization double num = 25; // Variable declaration double sqrtVal; sqrtVal = Math.Sqrt(num); Console.WriteLine("Square Root Value (" + num + ")" + ": " + sqrtVal); // Waits for any key press Console.ReadKey(); } } public class Test { public static void Main() { Console.WriteLine("Test Class"); // Waits for any key press Console.ReadKey(); } }
Output:
C:> csc SqrtCalc.cs Microsoft (R) Visual C# Compiler version 4.7.3056.0 for C# 5 Copyright (C) Microsoft Corporation. All rights reserved. This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240 SqrtCalc.cs(10,24): error CS0017: Program 'C:\SqrtCalc.exe' has more than one entry point defined: 'SqrtCalc.Main()'. Compile with /main to specify the type that contains the entry point. SqrtCalc.cs(24,24): error CS0017: Program 'C:\SqrtCalc.exe' has more than one entry point defined: 'Test.Main()'. Compile with /main to specify the type that contains the entry point.
here we have not specified which class Main method needs to be used during compilation.
We need to specify which class Main method to be used to start the execution using '/main' in C# compilation command.
Output:
C:> csc SqrtCalc.cs /main:SqrtCalc C:> SqrtCalc Square Root Value (25): 5
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page