C
C#16mo ago
júlio

❔ C# methods

Why do I need to have a class or struct, so that I can have functions? In other words, why do I need to put functions inside classes or structures?
26 Replies
júlio
júlio16mo ago
Is it just the way the language was build? Functions must always be associated to some object?
blinkbat
blinkbat16mo ago
yes there are no first-class functions in c#
júlio
júlio16mo ago
If that is the case, are lambdas the way to go in c# so small functions that dont have to be directly related to any object?
blinkbat
blinkbat16mo ago
sure lambdas are great there are also static classes that can have static methods for reuse without instantiation
júlio
júlio16mo ago
Yeah, I was thinking about that, would be annoying if the language forced me to instantiate an object every time I want to call a function :)
Denis
Denis16mo ago
In C you do not need to associate a class/struct with a function
júlio
júlio16mo ago
I come from C++
Denis
Denis16mo ago
In F# you work with functions, even though it does support classes So what you are asking is just how C#, as an OOP language, is built
júlio
júlio16mo ago
Yes Pretty much I was curious to see the compiler didnt allowed me to declare functions outside classes
blinkbat
blinkbat16mo ago
I typically make a static class for helpers related by concern
public static class UserHelpers
{
public static void SomeMethodToReuse() {} // etc
}
public static class UserHelpers
{
public static void SomeMethodToReuse() {} // etc
}
júlio
júlio16mo ago
no syntax sugar to avoid having to write static all over?
Denis
Denis16mo ago
A great example of a static class is Console
blinkbat
blinkbat16mo ago
not that i'm aware of and when using you need using static
Denis
Denis16mo ago
You do Console.WriteLine("hello") without creating an instance of Console
júlio
júlio16mo ago
Could be a Singleton
blinkbat
blinkbat16mo ago
namespace System
{
public static class Console
{
namespace System
{
public static class Console
{
it's not though
Denis
Denis16mo ago
No there is not. However you can omit the static keyword on a class and only have the methods static
júlio
júlio16mo ago
I see I will experiment with that a bit
Denis
Denis16mo ago
Happy coding
júlio
júlio16mo ago
Thank you both :)
Anton
Anton16mo ago
"first-class member" is a term that means "can be treated as an expression", which basically means "can be assigned to a variable and passed around" which C# supports in the form of delegates. Functions without a class are usually just called functions, while functions inside a class are usually called methods. Technically speaking, C# doesn't have functions. Even local functions are converted to regular methods by the compiler (I think)
Jayy
Jayy16mo ago
it depends on context, lambdas are common in many places
Angius
Angius16mo ago
There's one scenario where you don't need to put method inside of a class explicitly, and that's when using top-level statements. You're limited to just a single file then, though.
Console.WriteLine($"Hello {Foo()}");

string Foo()
{
return Random.Shared.Next(10) > 5
? "World"
: "Galaxy";
}
Console.WriteLine($"Hello {Foo()}");

string Foo()
{
return Random.Shared.Next(10) > 5
? "World"
: "Galaxy";
}
júlio
júlio16mo ago
Yes, the first time I created a project I didn't notice I had top level statements checked and it felt like a scripting language ahahah I see what you mean tho, that is interesting
Angius
Angius16mo ago
Yep, that's the goal of TLS Making quick little projects without the whole ceremony
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.