-
C# method
Hello,
Been looking into learn C# programming so i can get a career into programming, my question is trying to wrap my brain around method, if anyone can give me an easy example of a method such as passing a int or string from a created method and moved the parameter to the main method, and explain a little (very easy terms if you can because i am still new). Thank you, my code is below but it is not finished, if you can finish it as an example and explain what i missed that would be helpful to if not its ok.
Code:
class Program
{
static void second (int number)
{
int one;
string ones;
Console.WriteLine("please enter first number");
ones = Console.ReadLine();
one = Int32.Parse(ones);
}
static void Main()
{
Console.WriteLine("printing out different method: "+ );
}
}
}
-
anything about methods that can help me understand in your words maybe? trying to help would be appreciated
-
I don't really understand the question. Are you saying you want to return a value from a method, that's quite easy using the
Code:
static int second(int number)
{
return number +14;
}
In simple terms
-
where did you get return number +14;? indigio?
-
Siten
In this case he is returning whatever number is (see it being passed in the methods argument?) + the number 14 back to whatever called it.
You can think of a method like a "special" function encapsulated inside a "class". Really to simplify it just think of it as a function. You define what arguments it takes, then in that function/method you tell it what to do with those arguments, and finally you spit something back (unless the return type is void!)
-
See where the 'void' is? That's your return type. That could be an int, string, bool, whatever. Inside the standard brackets you can have whatever data you need be fed in to your method, or even nothing at all. If your method has been defined as anything other than 'void' you must make a return like 'return myResult;'
Code:
static void Main()
{
Console.WriteLine("Testing this...");
string myText = MyOutput(4, 8);
// the following line can also be written as Console.WriteLine(MyOutput(4, 8));
Console.WriteLine(myText);
}
string MyOutput(int numberOne, int numberTwo)
{
return numberOne + " x " + numberTwo +" = " (numberOne * numberTwo);
}
-
Though don't forget, just because you have a void return type doesn't mean you can't return. Returning from void doesn't return a value but "jumps out" of that function and returns back to the line after it was called. It differs from functions that return values by not having what you want to return after the return keyword.
for example
Code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Before Method Call");
divisByTen(2);
Console.WriteLine("After Method Call");
}
static void divisByTen(int start)
{
while(true)
{
if((start % 10) == 0)
{
Console.Out.WriteLine(start+" is divisible by 10");
return; //jumps out of the method
}
else
start++;
}
}
}
Also as a side note, you can't refer to non-static methods or variables from within static methods. In DanFraser's example it would be an error noting that. Non-static functions can refer to static or non-static members fine.