Evyr
Evyr
Explore posts from servers
CC#
Created by Evyr on 10/5/2024 in #help
✅ Aspnet runtime not found when running, but present in dotnet --info (linux)
No description
9 replies
CC#
Created by Evyr on 8/21/2024 in #help
✅ Struggling to use custom comparer with a list of KeyValuePairs
I'm trying to sort a list of keyvaluepairs on the key. I've defined an IComparer using Comparer.Create, but the list after sorting is identical to before. I can't use a SortedList because that requires unique keys.
IComparer<KeyValuePair<float, int>> comp = Comparer<KeyValuePair<float, int>>.Create((a, b) => a.Key < b.Key ? 1 : 0);
List<KeyValuePair<float, int>> li = [
new(1f, 1),
new(1.5f, 2),
new(2.5f, 3),
new(0.5f, 4),
new(0.2f, 5)
];
li.Sort(comp);

foreach (var kvp in li)
Console.WriteLine(kvp.Key + ", " + kvp.Value);
IComparer<KeyValuePair<float, int>> comp = Comparer<KeyValuePair<float, int>>.Create((a, b) => a.Key < b.Key ? 1 : 0);
List<KeyValuePair<float, int>> li = [
new(1f, 1),
new(1.5f, 2),
new(2.5f, 3),
new(0.5f, 4),
new(0.2f, 5)
];
li.Sort(comp);

foreach (var kvp in li)
Console.WriteLine(kvp.Key + ", " + kvp.Value);
The output looks like
1, 1
1.5, 2
2.5, 3
0.5, 4
0.2, 5
1, 1
1.5, 2
2.5, 3
0.5, 4
0.2, 5
Whereas I'd expect it to be sorted in either ascending or descending order.
7 replies
CC#
Created by Evyr on 7/20/2024 in #help
Is it possible to change the return type of interface method to the type of the implementing struct?
Say I have the following code:
interface IBiggerThan
{
public abstract IBiggerThan GetBiggest(IBiggerThan compareTo);
}
struct A : IBiggerThan
{
float _a;
public A GetBiggest(A compareTo)
{
if (this._a > compareTo._a)
return this;
return compareTo;
}
}
struct B : IBiggerThan
{
float _b;
public B GetBiggest(B compareTo)
{
if (this._b > compareTo._b)
return this;
return compareTo;
}
}
interface IBiggerThan
{
public abstract IBiggerThan GetBiggest(IBiggerThan compareTo);
}
struct A : IBiggerThan
{
float _a;
public A GetBiggest(A compareTo)
{
if (this._a > compareTo._a)
return this;
return compareTo;
}
}
struct B : IBiggerThan
{
float _b;
public B GetBiggest(B compareTo)
{
if (this._b > compareTo._b)
return this;
return compareTo;
}
}
I want to have GetBiggest return an instance of the struct its implemented within. This code obviously won't compile, but I'm wondering if there's a way to implement the intent behind it. I want to avoid returning object or IBiggerThan and then casting it wherever this method gets called, instead I want the return type to explicitly be that of the implementing struct. Is this possible, or am I missing something important?
5 replies
CC#
Created by Evyr on 6/25/2024 in #help
Looking for more performant alternatives to reflection for iterating through properties of a class
I've been using reflection to get the values of each of seventeen properties. I'm generating these values based off of an int seed passed to the constructor and trying to do so for the entire range of int values, so it'll an estimated 10 hours to complete. I was wondering if there were any more elegant solutions than writing out a "sum += <property>" for each property. Thanks!
5 replies