C
C#2y 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úlioOP2y ago
Is it just the way the language was build? Functions must always be associated to some object?
blinkbat
blinkbat2y ago
yes there are no first-class functions in c#
júlio
júlioOP2y 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
blinkbat2y ago
sure lambdas are great there are also static classes that can have static methods for reuse without instantiation
júlio
júlioOP2y 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
Denis2y ago
In C you do not need to associate a class/struct with a function
júlio
júlioOP2y ago
I come from C++
Denis
Denis2y 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úlioOP2y ago
Yes Pretty much I was curious to see the compiler didnt allowed me to declare functions outside classes
blinkbat
blinkbat2y 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úlioOP2y ago
no syntax sugar to avoid having to write static all over?
Denis
Denis2y ago
A great example of a static class is Console
blinkbat
blinkbat2y ago
not that i'm aware of and when using you need using static
Denis
Denis2y ago
You do Console.WriteLine("hello") without creating an instance of Console
júlio
júlioOP2y ago
Could be a Singleton
blinkbat
blinkbat2y ago
namespace System
{
public static class Console
{
namespace System
{
public static class Console
{
it's not though
Denis
Denis2y ago
No there is not. However you can omit the static keyword on a class and only have the methods static
júlio
júlioOP2y ago
I see I will experiment with that a bit
Denis
Denis2y ago
Happy coding
júlio
júlioOP2y ago
Thank you both :)
Anton
Anton2y 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
Jayy2y ago
it depends on context, lambdas are common in many places
Angius
Angius2y 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úlioOP2y 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
Angius2y ago
Yep, that's the goal of TLS Making quick little projects without the whole ceremony
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server