can some one explain C#'s return in a more simple way

There are some really good answers here. I also wanted to chime in with my own as well.

One thing to know about return is that it does not have to return any value, unless a return type was included in the method declaration. To really understand the point of return, you need to look at the way a method is declared. There are several things that make up the definition of a method and thus many ways it can behave or be interacted with.

This is the syntax used to declare methods.

[access modifier] [optional modifier] [return type] [method call [the name]([parameter type parameter, parameter type parameter, .....])] { [method body - the code the method executes when called])

Let's look at these

  1. access modifier - public, private, protected etc. - defines what access level to give the method thereby regulating which parts of the application will be able to call it
  2. optional modifier - abstract, static, virtual - there may be more than one of these in some cases - this dgets into complicated subjects like inheritance etc.
  3. return type - this is the part of the method declaration that deal directly with your question. This defines the type of object, if any, that will be returned by the method. All methods MUST have a return type, but the return type "void" allows you to declare that the method will not return any object. It wouldn't be accurate to say that the method returns nothing, even when the return type is void. You'll see why in a minute.
  4. method call - how the method is called by other parts of the application - has two parts
    1. name -the text that identifies the method being called
    2. parameter list - this can be empty, meaning the method takes no parameters, or it can have as many parameters as you define. Each parameters type and name must be defined, then inside the method you refer to these parameters with those variable names. This defines the objects that must be "passed in" to execute the method's code. There are lots of nifty little things you can do here with additional keywords like out and ref, but that's for another discussion.
  5. method body - {} - everything inside the curly braces - this is where the magic happens and where the answer to your question lies.

So, what does return do? Why did I find it necessary to get into such a detailed explanation of such basic concepts in order to answer your question? Well, because the return key word, really is absolutely tied to al

/r/csharp Thread