C
C#2y ago
Owl

❔ Learning C#, and curious about advanced return statements...

I'm jumping right into the depts and wish to return a new object of 'Buffer' type and construct it with lambda. The instruction should be to create Buffer object and write .txt file to Buffer.contents. Can someone teach me how to make that happen with a single statement?
using System.IO;

public struct Buffer
{
public string[] contents;
}

public Buffer ReadFromFile(System.IO.File _file)
{
return &Buffer();() => _file.ToString();
}
using System.IO;

public struct Buffer
{
public string[] contents;
}

public Buffer ReadFromFile(System.IO.File _file)
{
return &Buffer();() => _file.ToString();
}
(new to C#, so please be gentle)
17 Replies
Henkypenky
Henkypenky2y ago
this seems not that beginner friendly for c# you can't pass a File as a parameter, it's static you can probably pass a path and then do var file = File.ReadAllBytes(path); this will give you a byte array
Cracker
Cracker2y ago
If you are looking a class that can be initialized with parameter as lambda, that means constructor is accepting a delegate/action
Henkypenky
Henkypenky2y ago
also the built-in type Buffer is static
Cracker
Cracker2y ago
for example
Henkypenky
Henkypenky2y ago
you can't instantiate it and it's pretty stupid to have a struct for it which is just an string array you might aswell just have an string array only
Cracker
Cracker2y ago
same example without lambda
Owl
OwlOP2y ago
Yes, it is probably pretty stupid. The intention was as an example, but thank you. And okay, I see. Thanks!
Henkypenky
Henkypenky2y ago
forget about simple statements, they are hard to understand if they are not a requirement also the built-in Buffer is enough for your needs
Owl
OwlOP2y ago
The question wasn't so much related to "buffer" type, but to whether it was possible to instantiate a new object, like in Golang you do:
return &type{a:"", b:""}
return &type{a:"", b:""}
for example
Henkypenky
Henkypenky2y ago
you can, as long as it's not declared as static
Owl
OwlOP2y ago
Ah, yeh that makes sense.
Henkypenky
Henkypenky2y ago
the built-in Buffer and File can't be instantiated think of them as extensions like File.ReadAllBytes
Owl
OwlOP2y ago
Yes, i understand you
Henkypenky
Henkypenky2y ago
and Buffer.GetX
Owl
OwlOP2y ago
Yeh, thanks! I understood m8 😉
Henkypenky
Henkypenky2y ago
for simplicity: static = belongs to the type non static = specific object
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