How to declare a method in Main C #?

0 votes
asked May 25 by Daniil Napuda (120 points)
How to declare a method in Main C #?

3 Answers

0 votes
answered May 26 by apeksha parsewar (140 points)
#include <iostream>

using namespace std;

  

int max(int x, int y)

{

    if (x > y)

    return x;

    else

    return y;

}

  

int main() {

    int a = 10, b = 20;

    int m = max(a, b);

  

    cout << "m is " << m;

    return 0;

}
commented May 27 by Daniil Napuda (120 points)
This is C/C++, not C#.
0 votes
answered May 27 by shyam (570 points)

You can declare a method by following way,

<Access Specifier> <Return Type> <Method Name>(Parameter list)

{

     Body

}

Example:

public int add()

{

    Body

    return integer value

}

0 votes
answered Jun 4 by Peter Minarik (9,980 points)

A simple HelloWorld would look like this, in C#:

namespace HelloWorld
{
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

Main itself is a method, so probably you don't want to declare more method in it. (Note: C# allows you to do that, they are called "local functions" or you can do "lambda expressions" as well.)

References:

  1. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions
  2. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...