+ 3
How can I understand methods?
I did not understand methods in C#. What I have to do?
4 odpowiedzi
+ 3
Methods are the same thing as Functions. They do a task, a change or a simple action.
ex:
public void Bark( )
{
Console.WriteLine("puppy is barking");
}
public int Sum( int x, int y )
{
return x+y;
}
As you see, methods are like functions in C and C++. Try to read more about it on the Csharp tutorial :)
+ 3
One thing you guys must understand is the difference between built-in methods from pre defined classes, and user defined methods.
Console.WriteLine( ) is an example of built-in method, where WriteLine( ) is a method coming from Console.
If you have a class Human, and you create a method there called Walk, when you create an instance of your class Human in the main program, you would write "instanceHuman.Walk( )"
That means your Walk method comes from the instanceHuman object, that comes from the Human class. Here's an example:
public class Human
{
public Human( ){ }
public void Walk( )
{
Console.WriteLine("lol I can walk");
}
}
public class LetsSeeItHappens : Human
{
static void Main(string[ ] args)
{
Human myHuman = new Human( );
myHuman.Walk( );
}
}