Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return string variable from Main()

Tags:

People also ask

Can main function return string?

In many languages such as C, C++, and Java, the main method/function has a return type of void or int , but not double or String .

How do you return a string from a function?

Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid. Note the use of const , because from the function I'm returning a string literal, a string defined in double quotes, which is a constant.

Can main method return string in C#?

No, we can't return string from Main method. The entry point can optionally only return an int value.

How do you return a string reference in C++?

Use the std::string func() Notation to Return String From Function in C++ Return by the value is the preferred method for returning string objects from functions. Since the std::string class has the move constructor, returning even the long strings by value is efficient.


I would like to return a string variable from my Main() method. I've returned int variables. But I'm not sure if it's possible to return a string variable from Main() when you exit the program?

Any ideas?

Here is my int code:

    public class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        int error = 1;

        return error;

    }

}

If I change int to string, I get this error: Program does not contain a static 'Main' method suitable for an entry point. So obviously this is not allowed. What is the correct approach?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!