tags:

views:

257

answers:

7

I know how to use a method and that is solves some task but I do not get what exactly are they good for. Every pages says "it is a block of code...blabla", but I have not seen any simple explanation that says what are the methods good for and why are so important. I would be very grateful if someone could give me clear answer. Thank you!

+11  A: 

Why are they important? Because they're the primary place where code lives. (Other member types such as properties, constructors etc can contain code, but they tend to contain less code.)

It's a slightly odd question to try to answer, to be honest, because they're so fundamental. What's the alternative to having methods? I suppose you could put all code in properties, indexers etc, but that would just be equivalent to having methods, in a less natural form.

If your question is really why it's better to separate out a large sequence of steps into separate methods, that's mostly about readability/maintainability... it's easier to understand, reuse and maintain code in small, well-named methods rather than a single giant method which tries to do everything your entire program needs to perform.

Jon Skeet
+1 (just want to see what happens after 999k :-)
ring0
+13  A: 

They assist in breaking logic up into small, related chunks of code. This is useful as it supports code-reuse, and creates easier to maintain and read code.

A good method does one thing, and that thing is described by its name. You can re-use methods to execute the same logic from multiple places without duplicating it.

Access modifiers on methods allow you to control who can execute which method.

Michael Shimmins
+2  A: 

Chunk of reusable code capable of doing one task. In programming a method generally resembles a verb. For example, GetDate(), Show(), ValidateUser() etc.

Nadeem
+1  A: 

A method is a behaviour of a class.

We call them methods to differentiate them from functions, because functions do not need to be encapsulated in objects (conceptually at least. In c# I don't think you can have a function that is not a method.)

When designing your class, you should be thinking about what behaviours that class will perform in your application. For example if I have a Client class, that class could have the behaviours Login, Logout, Buy Service, etc.; these behaviours inform what methods your class will have, and will inform the names of your methods.

So, the reason methods are important is that they are the 'doing' parts of a class. Without methods, classes are just collections of data (granted that in c# properties and indexers have method like characteristics, but generally they don't tell you what a class does, just what it has in terms of data), so you could just substitute a Dictionary<string, object> for your class and be done.

Matt Ellen
A: 

Well, I try to answer:

You probably know that classes generally serve for creating objects that represents independent elements. Thus you can logically structure your program. In the real world, every object has some purpose which means it is good for something, it can do something. This means it can do an operation specific to it.
And this operation of that object is actually method, i.e. group of comamnds with specific name (reflecting its purpose) that can be called (executed) to do this operation and, if appropriate, return the result.

Ptr
+4  A: 

Let me try it a little bit more of a real world example, maybe this will help u?

Methods discribe what a object can do, for example we have a Car. What can a car do?

exactly, Drive!

So one of its methods is Drive().

Maybe it can open its doors, so there is a Opendoor() method for that.

But now you don't know which door to open, so u specify this by giving a method a ' parameter' . For example the Opendoor method can accept one parameter which tells the method which door to open.

For example Opendoor("Left") will open the left door while Opendoor("Right") will open the right door. So now u see that Opendoor has been used twice for the same action, but u only had to code it once!

In short,

Classes are Objects, or Nouns in language.

Methods are the actions a object can do, or Verbs in language.

Hoped this helped!

ps: U should go and buy a good starting book, it will answer excatly these sort of questions.

Emerion
+8  A: 

The explanation by Jon Skeet is really good. I would like to give a simple example. Suppose you need to make a coffee. Steps involved would be: Take some milk in a container, warm it, add coffee and sugar, make it hot, pour it in your cup and your coffee is ready. You can write these steps everytime your program needs to make a coffee, so it would be copying these many steps again and again and pasting everytime. Rather you can just put these steps in a Method say "MakeCoffee()" and call this method whenever you need to make coffee.

There are many other implementaions and uses, I just gave this simple example to have a ver basic understanding. For further reading I would suggest a good OOPS book.

It increases readability, easy to maintain, easy debugging, well structured as well as avoids code duplication. I hope it helps:)

sumit_programmer