substitute
substitute
CC#
Created by substitute on 7/31/2024 in #help
new Span vs stackalloc
I'm working on a library that is able to read and write memory from a remote computer and was wondering which of these two is better practice?
public void WriteFloat(uint address, float value)
{
Span<byte> memory = stackalloc byte [sizeof(float)]
BinaryPrimitives.WriteSingleBigEndian(memory, value);
SetMemory(address, memory, out _);
}
public void WriteFloat(uint address, float value)
{
Span<byte> memory = stackalloc byte [sizeof(float)]
BinaryPrimitives.WriteSingleBigEndian(memory, value);
SetMemory(address, memory, out _);
}
public void WriteFloat(uint address, float value)
{
var memory = MemoryMarshal.Cast<float, byte>(new Span<float>(ref value));
BinaryPrimitives.WriteSingleBigEndian(memory, value);
SetMemory(address, memory, out _);
}
public void WriteFloat(uint address, float value)
{
var memory = MemoryMarshal.Cast<float, byte>(new Span<float>(ref value));
BinaryPrimitives.WriteSingleBigEndian(memory, value);
SetMemory(address, memory, out _);
}
I feel as though these would produce extremely if not exactly the same memory layouts, it creates a Span in both.
76 replies
CC#
Created by substitute on 9/28/2023 in #help
❔ Is it possible to use a generic type as another type outside of the generic class w/o reflection?
Hi, in C++ we can write code like the following:
#include<iostream>

class test1 {}; //example, can be anything.
class test2 {}; //example, can be anything.
class test3 {}; //example, can be anything.

template <typename T1, typename T2, typename T3>
class tuple
{
public:
using First = T1;
using Second = T2;
using Third = T3;
};

using my_tuple = tuple<test1, test2, test3>; //Alias tuple<i,j,k>

int main(){
my_tuple::First first_ele; //Variable of test1 derived from T1 on my_tuple alias
my_tuple::Second second_ele; //Variable of test2 derived from T2 on my_tuple alias
my_tuple::Third third_ele; //Variable of test3 derived from T3 on my_tuple alias

printf("%s\n", typeid(first_ele).name()); //prints the type test1
printf("%s\n", typeid(second_ele).name()); //prints the type test2
printf("%s\n", typeid(third_ele).name()); //prints the type test3
}
#include<iostream>

class test1 {}; //example, can be anything.
class test2 {}; //example, can be anything.
class test3 {}; //example, can be anything.

template <typename T1, typename T2, typename T3>
class tuple
{
public:
using First = T1;
using Second = T2;
using Third = T3;
};

using my_tuple = tuple<test1, test2, test3>; //Alias tuple<i,j,k>

int main(){
my_tuple::First first_ele; //Variable of test1 derived from T1 on my_tuple alias
my_tuple::Second second_ele; //Variable of test2 derived from T2 on my_tuple alias
my_tuple::Third third_ele; //Variable of test3 derived from T3 on my_tuple alias

printf("%s\n", typeid(first_ele).name()); //prints the type test1
printf("%s\n", typeid(second_ele).name()); //prints the type test2
printf("%s\n", typeid(third_ele).name()); //prints the type test3
}
This effectively allows a special type-of-types to exist that only exists as a way of tying together types into a singular type (tuple<i,j,k>) while allowing access to the inner types i,j,k at compile time. I was wondering if there was an equivalent in C# that didn't require reflection. In essence, I would like to do something like this
using CreateAccount = ReqResErr<Request.CreateAccount,Response.CreateAccount,Error.CreateAccount>
using CreateAccount = ReqResErr<Request.CreateAccount,Response.CreateAccount,Error.CreateAccount>
to later consume like
Envelope.Open<CreateAccount>
Envelope.Open<CreateAccount>
which does like
T1 Req = T0::First
T2 Res = T0::Second
T3 Err = T0::Third
T1 Req = T0::First
T2 Res = T0::Second
T3 Err = T0::Third
Thanks!
139 replies