knut.wannheden
knut.wannheden
CC#
Created by knut.wannheden on 7/1/2024 in #help
✅ dotnet.microsoft.com is down
Let's hope it isn't for too long...
6 replies
CC#
Created by knut.wannheden on 5/16/2024 in #help
Definitive C# language reference?
No description
22 replies
CC#
Created by knut.wannheden on 5/14/2024 in #help
Generic types API design
Coming from Java I am used to generics having type erasure at runtime and also wildcard types (like e.g. Foo<?>) at design time. In C# the type parameters are reified and take some getting used to. I am working on a framework where I have types using generics and also API methods which declare parameters of these generic types. But in many cases the methods don't really have to care about the type parameters, which is when I in Java would have declared the method parameter using a wildcard, rather than adding a type parameter to the method signature. Now I am wondering how this is typically approached in C#. What I ended up doing, but probably isn't what you are supposed to do, is that I declared an interface IFoo without type parameters and then another one with type parameters as IFoo<T> : IFoo. Now I can declare method parameters of type IFoo (or IFoo[] or similar) when I don't need to call methods which require the type parameters. I suspect that this is probably more of an anti pattern and I would like to learn how to better design APIs in C#.
5 replies
CC#
Created by knut.wannheden on 5/13/2024 in #help
Best practice reading from `Socket` / `NetworkStream`?
I've got a C# application in which I want to read data from a Unix domain socket (on the sending side I have a Java application which sends buffered data). I want to read all data into a MemoryStream, as I subsequently want to parse that using CborReader (unfortunately I didn't find any way getting the CborReader itself to read the data directly from the socket). Now, to my question: I can't figure out what the best practice should be for fully reading the data from the Unix domain socket into a MemoryStream. My tests indicate that I can't rely solely on NetworkStream.DataAvailable but also need to call Socket.Poll(), so I've come up with the following code:
private static MemoryStream ReadFully(Socket socket, NetworkStream stream)
{
var memoryStream = new MemoryStream();
var buffer = new byte[8192];
while (socket.Poll(TimeSpan.FromMilliseconds(100), SelectMode.SelectRead))
{
while (stream.DataAvailable)
{
var bytesRead = stream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, bytesRead);
}
}
memoryStream.Position = 0;
return memoryStream;
}
private static MemoryStream ReadFully(Socket socket, NetworkStream stream)
{
var memoryStream = new MemoryStream();
var buffer = new byte[8192];
while (socket.Poll(TimeSpan.FromMilliseconds(100), SelectMode.SelectRead))
{
while (stream.DataAvailable)
{
var bytesRead = stream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, bytesRead);
}
}
memoryStream.Position = 0;
return memoryStream;
}
Does this make sense or is there a better / more idiomatic way?
12 replies
CC#
Created by knut.wannheden on 4/23/2024 in #help
primary constructor naming convention
Hi! I was wondering if there is a naming convention for primary constructor parameters on a class. Let's say I have class Point(int x, int y) and now I want to add a method to this class with an int x parameter. AFAICT I then can't really resolve the name conflict between the two in the method body, so I was wondering if I should rename the primary constructor parameters to e.g. _x and _y or if I should instead rename the method parameter to avoid the conflict. Thanks!
10 replies