Stand alone function question

thetexan

New member
Joined
Dec 3, 2014
Messages
3
Programming Experience
1-3
Must all c# code be contained within a class?

Can I create a stand alone function outside of a class that is called by a method inside a class?

If that's the case then it seems to me that if I have a class from which I make hundreds of objects such as an employee class, and within that class there is a method which creates a unique employee ID, rather than creating hundreds of employee objects each of which with a duplicate method which takes up memory, that it would seem better to create one function that does the same thing that is called from each object.

Of course this wont work if there are no stand alone functions.

tex
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,150
Location
Sydney, Australia
Programming Experience
10+
There is no such thing as a standalone function. All methods must be members of a class or structure. What you're trying to do is misguided. Just do what every developer the world over, including the authors of the .NET Framework, always do. A method goes where it logically belongs in source code. Low level optimisation like your talking about is someone else's responsibility, not the application developer.
 

Infinito

New member
Joined
Dec 6, 2014
Messages
1
Programming Experience
Beginner
it is good if we can do that but we have hierarchy in coding, like jmcilhinney mentioned every thing should be under a class.just wondering why you have thought about it, we can just create a method that does what you want. why create so many instances?
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Must all c# code be contained within a class?
Yes

Can I create a stand alone function outside of a class that is called by a method inside a class?
Yes and no, you can create a 'stand alone function' but it's still part of some class. What you're looking for are static methods.

class MyFunctions
{
    public static int multiply(int x, int y)
     {
         return x*y;
     }
}

You can access this from any class or code via
    int c = MyFunctions.multiply(a, b);

If that's the case then it seems to me that if I have a class from which I make hundreds of objects such as an employee class, and within that class there is a method which creates a unique employee ID, rather than creating hundreds of employee objects each of which with a duplicate method which takes up memory, that it would seem better to create one function that does the same thing that is called from each object.
I find this an interesting question and I'm glad that somebody thinks about this type of things and does not take it for granted. I do not have the knowledge to answer it but I think (I know that that is dangerous) that a non-static method is only loaded once in memory and shared between the instances; each instance will only contain a pointer (reference) to the method.

// Edit: did a little research and found this for c++: http://www.cplusplus.com/forum/general/31673/
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,150
Location
Sydney, Australia
Programming Experience
10+
No, any code inside a structure is not inside a class. Enumerations and delegates are also first class types in their own right and can therefore be declared anywhere a class or structure can. All methods must be declared inside a type, but that type can be a structure or a class.
Yes and no, you can create a 'stand alone function' but it's still part of some class. What you're looking for are static methods.
Not necessarily. While a static method might be construed as a "standalone" function, there's nothing in the OP that specifically suggests that a static method is more appropriate than an instance method in this case.
 
Top Bottom