December 11th, 2011, 12:04 PM
I'm having a hard time wrapping my head around the OOP design of C# and how to correctly use methods.
Say I want to create a function that will be called multiple times(which I guess is always called a method in C#?).
I obviously don't want to put it in my main class, since I will call it more than once.
I can't seem to make any function outside of a class .. anything that happens in C# happens inside a class or structure type, period?
If I want to put my algorithm outside of my main function, which doesn't have any variables and which simply needs to be called (uses 2 parametres), I create a static class?
If any small function would reside in a class which uses a reference, wouldn't that create a lot of overhead?
Last edited by Candystore; December 11th, 2011 at 12:08 PM.
December 11th, 2011, 06:06 PM
Re: Methods, confused!
You should put your methods (functions or subs) in the class that makes the most sense. That could be your main class, a public class or a static class depending on how it is to be used within the program.
I typically place related functions in the same class so they are kept together and easy to re-use. It doesn't matter if your function needs to be called once or a thousand times as to where it should be. It does matter if it is going to be called from different threads and/or if it uses shared data.
In the case above you could put your function in your main class or a static class or a public class. If the latter then you must create an instance of the class before you can access the methods of that class and of course the function must be public if it is to be accessed from a different class.
Always use [code][/code] tags when posting code.
December 12th, 2011, 12:31 AM
Re: Methods, confused!
One main takeaway is instance methods access class instance data. Static methods do not have access to class instance data (but they do have access to static class data). Generally data is passed to static methods as parameters.
Last edited by Arjay; December 12th, 2011 at 12:34 AM.
December 12th, 2011, 08:38 PM
Re: Methods, confused!
Classes are basically a way to define new types (as are structs, but let's forget about structs for now). Instances of those types are called objects. (So, a class - a type - is a blueprint for an object.)
In OOP, without getting into nuances, a type simply defines what an object of that type can do (via methods, a.k.a functions), and what internal data it contains.
Simply put, classes are a way to bring some data and operations related to that data together.
But the real power of classes is that they enable you to create new types to represent various concepts required by your application (like a bank account, or user data, or a video game entity, or a bitmap image, or whatever).
C# is more OO than C++, and as such, it doesn't allow you to define free functions.
You can, however, define static methods, and even static classes; but for the most part, you wont be using these - you'll be using objects and their related methods (a.k.a. instance methods).
Instance methods operate on the data of their respective objects.
Static methods, in a sense, belong to the class, and not specifically to any of its objects - they [static methods] are shared among all objects of the same class (as are any static members).
OOP is all about reuse, so nothing is preventing you to reuse any method multiple times. You've used objects in your code, even if you didn't realize it, because, in C#, everything is an object (again, nuances aside).
You must have used things like ints and strings - these are all proper objects and have methods associated with them.
You simply create an instance of a certain class, and then you can call any method defined on it, as long as the variable is "known" to the calling code (otherwise, you can pass this object around to other objects and methods - as parameters for example - which is a way for different objects to communicate).
Here's a simple example:
So, instead of having a bunch of variables that together represent something, and are accessed or altered by a bunch of free functions (which can become a real pain really fast), you define classes that bring them together and are then able to do everything in a more controlled fashion.
// The car class defined somewhere...
public class Car
private string _color = ""; // internal data
// constructor - creates the object
public Car(string color)
_color = color;
public void PrintInfo()
Console.WriteLine("This car is " + _color + "!");
// In the Main() method...
Car myRedCar = new Car("red");
// Now, you can call the PrintInfo() method anywhere in your code where
// myRedCar variable is available (where it's in scope), as many times
// you like.
// Prints out: This car is red!
Now, it is possible to write non-OO code in C#: for example, what I just described above using free functions, can be completely reproduced in C# using static methods and static variables - but that then completely defeats the purpose of both C# and OOP, and is bound to turn into a mess (especially as your projects become more complex).
With that in mind, static methods are still useful. These are usually some kind of general purpose, utility methods (often in a static class, like is the case with the Math class, which provides support for a bunch of math operations). Sometimes, static methods are used instead of constructors to create objects, as it is possible to give them any name (while a constructor always has the same name as its class), and thus the code can be made more clear. Or to parse strings to get other types - for example, the Int32 structure (which is what the int type actually is under the hood) has a static Parse() method which accepts a string, and converts it into a number which it then returns as the result.
There are some other uses as well.
In C#, a static method is accessed via the name of its class (this reflects what I said earlier - that you can think of a static method as if belonging to its class), like this:
int max = Math.Max(x1, x2);
BTW, a static class is one that cannot be instantiated, and can only contain static members.
Now, for simple, experimental console applications, for study purposes, you could add new static methods to you Program class, to complement your existing Main() method, and call them from Main(). If you're calling a static method from the same class, you can just type in its name - you don't have to access it through the class name.
Something like this:
But, take my advice, the more you know about classes and OOP, and the sooner you start using them properly, the better.
// You can define new methods anywhere in the file, the order
// is not relevant in C#.
public static void Main(string args)
// Call the other methods here
// let's add some other static methods...
public static void SayHello()
public static void SayMyName(string name)
Console.WriteLine("Hello there, " + name + "!");
P.S. All the code is typed in and not tested, so I apologize if there's a minor bug or two - but, you should be able to get the point.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!