Jump to content
  • Advertisement
Sign in to follow this  

Are there two definition for methods or what?

This topic is 1112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm learning C# (dumped Python) and in the tutorial, I learned that the thing below is a Method...

static void main(string[] args) 

But then it says that WriteLine is a member of the Console and that it is also a method. I'm confused. Are they both methods? Or are they two different types of methods?

Share this post

Link to post
Share on other sites

Both are methods.


In programming in general, a method is a chunk of code that takes zero or more arguments and returns zero or more values.

Depending on what language you are using, they can also be called functions, subroutines, or several other names. Sometimes, in some languages, the different words are given slightly different definitions to differentiate different meanings. Usually (but not always), 'method' means the function is attached to a class and has access to that class' private variables.


In this case, static void Main(string[] args) belongs to whatever class you put it in, and is a method that returns nothing (aka void), and takes one argument ('args'). That one argument is an array (of zero or more elements) of strings.


'Console' also is a class. In the same way 'Main()' belongs to the class you create, 'WriteLine()' belongs to the 'Console' class. WriteLine also happens to returns void (nothing).

But there's a trick: The 'Console' class actually has about a dozen different functions, all with the name 'WriteLine()' that all return void. blink.png

But this is actually useful; each of the same-named functions take different arguments, and so depending on which arguments you pass the function, different versions of the same function will be called (this is called "overloading" a function). This way, you can pass WriteLine() a string if you want to write a string, or you can pass WriteLine() an Int if you want to display an int, and so on.

Instead of having to remember WriteLineForString(string) and WriteLineForInt(int), you just say WriteLine(blah) and it automatically detects whether it's a string or an int (or a dozen other things).


But anyway, both Main() and WriteLine() are methods (a chunk of reusable code that possibly takes arguments and possibly return values), and both are members of a class.

Share this post

Link to post
Share on other sites
The word "define" itself also means something special in programming which might be confusing you. It means "Writing a piece of a program and giving it a name." Sometimes you might see the word "identifier" instead of "name".

In this instance:

"main" is the name of the method.
"void" is the return type of the method (the type of output the method is promising to return under normal circumstances).
"static" is optional and means that the method can be called using the class name instead of an instance of the class.
"(string[] args)" is the "parameter list" for the method (the inputs).
The code inside the { } which come after the parameter list is the "body" of the method.

When you have all of these things, it is the "definition" of that method. Each method has a definition *somewhere*, whether it's in code you write, or code someone else writes.

Similarly, a definition of a variable consists of the variable's type, and a name for the variable, such as "int someNumber;", and a definition of a class includes the class name, base class, interfaces, all of its methods, variables, properties, and anything else that can be defined inside a class. Edited by Nypyren

Share this post

Link to post
Share on other sites

"Writeline is a member of console" just means


"Writeline is part of console"


Class is a broadly defined logic-chunk. You can make your methods part of that broadly defined logic.

So example:
Class Head
    Class Eye
       string color ;
          //Some code to control seeing
    Class Hair
       string color = "black" ;

So head is a class - eye is a class, which is a member of head.


See() is a method, which is a member of eye.


To "Call" See(), you'd write Head.Eye.See()


If you make "Head" a "static" class, that means there's only 1 head in existence, or your singular code controls all members of Head in unison. So if you call "Head.Eye.See()" all it would effect all of them in your program.


But, if you make "Head" a dynamic class (just don't use the keyword static), then you can tell a specific head to See().


The power of Object Oriented Programming is in dynamic classes because you can create instances of these logic chunks. (instances means copies essentially)


So if you want 2 heads you could do something like this:

Head Hank = new Head() ; //now you'v got an instance of Head, that also contains the members, Eye, Hair.
Head Sarah = new Head() ;
Hank.Hair.color = "red" ; //you just gave Hank red hair. 
Hank.Eye.color = "blue" ;

//You didnt have to create the new Eye or Hair, because the new Head inherited them, since they're part of the Head Class. Above, when I gave them colors, I was just assigning values.

//however, right now Sarah's hair is "Black" because that's what was assigned by default, and her eye color is null, because nothing was assigned in the class.

//Make Hank see.
Hank.Eye.See() ;

Basically, Classes are logical topics which don't really "do" anything. They hold other information, kind of a mirror of real life. Words are symbols that hold meaning to describe functions, just as Classes hold Methods which.. do stuff.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!