What is the difference between "


" and "


Also explain(efforts will be appreciated):
What does



Also explain(efforts will be appreciated):
What does
C# Syntax (Toggle Plain Text)

I meant




This, when running a windows forms application, will display a popup message box on screen displaying the information you have passed it within the brackets.

return x;

Return is used to send a variable back from a method to the block of code that called the method. It is a required line of code for methods that are not declared as void (returns nothing)

For example:

int a = 10;
int b = 5;

int d = Addition(a, b);

public int Addition(int Num1, int Num2) //Method returning a parameter of type Integer
    int c = Num1 + Num2;
    return c;

In the above example we have two integers, a and b, which I am passing to a simple addition method that will add them together and give me back a value to set integer d too.

In this method the return keyword plays the role of passing the value of 'c' back to the original location the method was called from, in this case line 4.

Edited 4 Years Ago by Mike Askew: Code comment

return may be used in void methods, but may as well be omitted.

public void TestMethod()
            MessageBox.Show("This is a void method that shows a message and returns nothing");
            return; // may be omitted
// use:
TestMethod();// will show a message

Edited 4 Years Ago by ddanbe: n/a

he asked differce between that you answer thae return statement usage only

return can also be used as flow control in methods, even void methods

public void TestMethod() {
    if (someStatus == true) return;


You use this to remove decision making from the calling method and placing it within the called method. This is useful when the information you need to make the decision is hidden from the calling method as you don't need to add more visibility to things that don't need it (Information hiding).

Ty for correcting me, learnt something :D

return means you want to return "something" from your method.

for example:

you have a method called "addNumbers"

here it is:

private void addNumbers(int a, int b)
    return a + b;

once you call the method by passing values:

addNumbers(9, 8);

it will "return" a value "back" to you!

if you know for a fact that you want to get a value from a method:

you use "methods that returns a value"

otherwise, use void method.

just like the example you gave.


OK. Thank you for all the replies you guys gave. So return basically "gives back a value" to the line that called the method. So in this kind of syntax in a class called "main.cs"--

public static int Subtraction(int num1, int num2)
    int result = num1 - num2;
    return result;

And then later I call the method like this on a button click---

int sub = main.Subtraction(5, 3)
MessageBox.Show("5 - 3 is equal to " +sub)

return value is given to the line

int sub = main.Subtraction(5, 3)

and then used in the line

MessageBox.Show("5 - 3 is equal to " +sub)

So if the


line wasn't there, the return value would not have been displayed?

Also explain:
I just learnt that staticmethods can be used without making any objects. So-
1. Why wouldn't I want ALL my classes and methods to be static?
2. Why is Main() static?

1. Static classes can only exist as singleton classes. Say you wanted a class for monsters in a game. If that class was entirely static, each monster would share the same members and methods. Therefore all monsers would occupy the same coordinates, life, etc. Static classes and methods are usually used as helper classes or functions that are common to all of the classes instances. For example, a class that you use to load data from a settings file. You only need 1 instance of this class, and it just helps keep all of your settings file operations in a neat little class.

2. Main() is static since it is common to all instances of your program. Your application itself is a static class, and so is its application domain. But your main() function is always the same between all of them. It basically boils down to your compiler needing an entry point into your program. You can put the [STAThread] static main() anywhere in your program, try putting it in your main form in a windows app. Since it's static it doesn't need an instance of the class to exist, and can be used as the entry point from within any class.

Edited 4 Years Ago by skatamatic: n/a


Since it's static it doesn't need an instance of the class

So if I change the "Main()" in my "Program.cs" to "NotMain()" or whatever and i place Main in another class called "ThisIsMain.cs", ThisIsMain becomes my entry point?

Not automatically, you'd have to tell the compiler where the Main method is found (it's automatically set when you create the project).

I use Visual Studio 2010, and this information is found at Project-><app name> Properties on the Application tab. It's the Startup Object dropdown.

It's possible that the system would find the Main method on its own, but this ensures that it finds the right one.

Ok. I get all this return business. But what does return 0 do?

Returns a value of 0 to the calling method...

Why will I even need return 0;?

You don't. In C++ most compilers require an int main(), whereas in C# I am pretty sure all compilers support a void main().

A return value of 0 from your main() typically indicates that the program executed properly and returned with no error.

you can use the messagebebox.show to show your result

but even though you just called the method

behind the scenes it actually returns the value back

in the other hand you can output the method into controls such as textbox, etc

or you can put the value returned back into a variable for later processing

and for the static

sometimes you want to hide your methods from the user or to other programmer

so if you do, you dont use static method

they must know the class name in order to call your method

hope this helps!

Hey dude

Return()--function used to return the single (note:only one) value to the calling method
MessageBox.Show()--use To Create a new window that display which message we gave as input when we press ok button in that window window will closed it is precreated template for message box
Retern-- may be an use created variable or object name or class name or structure,interface,enum name or any other user defined or it may be user defined alias for any function or variable

return is used to return a value from the method, Suppose if x is an integer valve in any method named xImplement() then return x; will return the value x to its caller. And if no value is returned i.e. return; is used without any values then return is used to prevent the further execution of code.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.