Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to declare a method in Main C #?

+1 vote
asked May 25, 2020 by Daniil Napuda (130 points)
How to declare a method in Main C #?

3 Answers

+1 vote
answered May 26, 2020 by apeksha parsewar (160 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, 2020 by Daniil Napuda (130 points)
This is C/C++, not C#.
0 votes
answered May 27, 2020 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, 2020 by Peter Minarik (84,720 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.
...