Reputation: 45
I'm new to C# still, so bear with me here.
If I want to run a simple algorithm with just 2 parameters, in other languages I would simply create a function outside of my main()
. Instead, in C#, apparently I need to create a class and put the function (method) inside that class, and make the method static so it's coded for runtime instead of having to create an actual object of the class, correct?
If all that is true, wouldn't it create a lot of overhead, since you are creating a reference to something?
Upvotes: 0
Views: 567
Reputation: 93424
There is no need to create an additional class, your main method is a static method in your Application class. You can add a method to this class and call it from main, but that method has to be static as well.
If you want to take advantage of object oriented features, like member variables then you need to create a class. This class should have a method, and does not need to be static.
Static methods do not contain any overhead of object creation, they're just function calls. Functions have some overhead (either static or non) because they have to push objects on to the stack and pop them off when exiting. However, this overhead is generally miniscule and only relevant in very tight loops that need optimimum performance.
In general, you shouldn't worry about it unless it's critical to your app.
Upvotes: 3
Reputation: 121649
1) But in C# I need to create a class and put the function (method) inside apparently, and make it static...
Yes, that's correct.
2) make it static so it's coded for runtime instead of having to create an actual object of the class, correct?
No, not really. In this context, "static" simply means "class-wide: independent of any particular class instance" ("class instance" == "instantiated object").
3) wouldn't this create overhead, since you are creating a reference to something?
Nope. EVERYTHING in your program exists SOMEWHERE. In C#, that "somewhere" just always happens to be within one or more "classes". And EVERYTHING you use has a "reference" (equivalently, an "address"). You're definitely not creating anything "extra", and there's certainly no "runtime penalty".
'Hope that helps ... at least a little bit... :)
Upvotes: 2
Reputation: 112334
Unless you are writing very very small programs, you will always have to use several classes (static or not). I urge you to aim a well-structured, well understandable and clean programming style, instead of trying to save a nanosecond here and there.
Upvotes: 1
Reputation: 47375
Your other languages have references too. Maybe they just hid them from you.
Having a static method actually can consume less resources, because the method is attached to the class, not an object instance on the heap. So, there will be only 1 method put into memory by the class loader.
Upvotes: 0
Reputation: 83358
In c# they're called methods, not functions.
But yes, if you don't want to create a new class, but just call a method directly from your Main(), then you would create a static method in the same class as Main, and call it therefrom.
As far as overhead, I don't think you should spend too much time worrying about that unless you're building some sort of device driver.
Upvotes: 6