In C/C++, I have a bunch of functions that I call from main(), and I want to rewrite this in C#. Can I have stand alone functions(methods) or do I have to put them in another class? I know I can have methods within the same class, but I want to have a file for each function/method.
Like this works:
using System.IO; using System; class Program { static void Main() { House balls = new House(); balls.said(); } } public class House { public void said() { Console.Write("fatty"); Console.ReadLine(); } }
But then I have to create an instance of House and call said(), when in C I can just call said().
A standalone function is a function (a subprogram that returns a single value) that is stored in the database. Note: A standalone function that you create with the CREATE FUNCTION statement differs from a function that you declare and define in a PL/SQL block or package.
A stand-alone function is just a normal function that is not a member of any class and is in a global namespace. For example, this is a member function: class SomeClass { public: SomeClass add( SomeClass other ); }; SomeClass::add( SomeClass other ) { <...> }
There are two types of functions in C++: stand-alone functions and member functions.
3) There is no limit on number of functions; A C program can have any number of functions.
For reference, I want to add the using static
addition of C# 6 here.
You can now use methods of a static class without having to type the name of that class over-and-over again. An example matching the question would be:
House.cs
public static class House { public static void Said() { Console.Write("fatty"); Console.ReadLine(); } }
Program.cs
using static House; class Program { static void Main() { Said(); } }
No. Make them static
and put them in a static
utility class if they indeed don't fit within any of your existing classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With