Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i am working on a training console programme and right now i am trying to get a method to return a string from the Main Method. Like this:

C#
Method... {

   if (s = "string") {
       do that;
   }

}



C#
public static void Main(string[], args) {


       Console.Write("write something: ") // Types in "string"

       s = Console.readLine();


}


What I have tried:

Searching a bunch online. It is really hard to know what to search for if you dont really know how/where/what to search for. I have also tried to ask Programmers online, but its never free...
Posted
Updated 9-Apr-18 9:38am

A main function in C# can only return either int, or void type, nothing else. You cannot force it to return anything other than that, because of the legacy stuff—try reading why C or C++ programs start with int main() { }.

That said, in your code, you are not even trying to return anything. Instead, you are capturing input. Console.ReadLine() is used to capture the input and then process, such as the one you are doing—if(s = "string"). Now, to return something you need to change the return type of the function and you need to use the return keyword to return a value. You are not doing that anywhere. Any other function that returns a string would be like this,
C#
public static string Greetings() {
    Console.WriteLine("Enter your name: ");
    string name = Console.ReadLine();

    // returns the string.
    return "Hello, " + name + "!";
}

So, I would recommend going through C# documentation a bit more so that you can understand how this works. :-)

Main() Return Values (C# Programming Guide) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 13770588 9-Apr-18 15:22pm    
but i mean more like, if i create the method you displayed. Is there a way i can use the "name" in another function?.

like if i want Greeting to ReadLine then use it in another method depending on what i write?

Example: public static string name1() {

if (name = "Simon") {
// Code
}
}

Example: public static string name2() {

if (name = "Alex") {
// Code
}
}

public static string Greetings() {
Console.WriteLine("Enter your name: ");
string name = Console.ReadLine();

}
Afzaal Ahmad Zeeshan 9-Apr-18 15:42pm    
No, that is where scoping comes in. A variable is scoped in that function and only exists inside that function. You would otherwise need to create a property on the class—properties and fields is the topic you should look into. Those fields can be accessed from all the functions inside the class.
Member 13770588 9-Apr-18 16:08pm    
Thank you! :)
Ehsan Sajjad 10-Apr-18 3:47am    
well expalained. 5ed
Afzaal Ahmad Zeeshan 10-Apr-18 6:19am    
Thank you, Ehsan.
Quote:
now i am trying to get a method to return a string from the Main Method.

main returning a string does not exist. All what main can return is an integer and that integer is not for you, it is for the OS.

Really not clear what you want to do with that.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 9-Apr-18 15:44pm    
In C# it can return void too, and the fun part is that now it can return Task and Task<T> too, due to the support for asynchronous main functions—See Solution 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900