C
C#14mo 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
Henkypenky14mo 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
atakancracker
atakancracker14mo ago
If you are looking a class that can be initialized with parameter as lambda, that means constructor is accepting a delegate/action
Henkypenky
Henkypenky14mo ago
also the built-in type Buffer is static
atakancracker
atakancracker14mo ago
for example
Henkypenky
Henkypenky14mo 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
atakancracker
atakancracker14mo ago
same example without lambda
Owl
Owl14mo ago
Yes, it is probably pretty stupid. The intention was as an example, but thank you. And okay, I see. Thanks!
Henkypenky
Henkypenky14mo 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
Owl14mo 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
Henkypenky14mo ago
you can, as long as it's not declared as static
Owl
Owl14mo ago
Ah, yeh that makes sense.
Henkypenky
Henkypenky14mo ago
the built-in Buffer and File can't be instantiated think of them as extensions like File.ReadAllBytes
Owl
Owl14mo ago
Yes, i understand you
Henkypenky
Henkypenky14mo ago
and Buffer.GetX
Owl
Owl14mo ago
Yeh, thanks! I understood m8 😉
Henkypenky
Henkypenky14mo ago
for simplicity: static = belongs to the type non static = specific object
Accord
Accord14mo 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.