Gladiator
Gladiator
Explore posts from servers
CC#
Created by Gladiator on 1/22/2023 in #help
❔ Should dictionary values be value types (struct)
To store data container (just data), do you prefer to define dictionary values as value types (struct) or class?
15 replies
CC#
Created by Gladiator on 12/27/2022 in #help
❔ Consider struct or class types for small event args
Do you consider to define small event args (e.g. two floats or ints inside) as struct type to avoid GC especially for events which happen frequently?
12 replies
CC#
Created by Gladiator on 12/27/2022 in #help
❔ Nullable enums or None inside
Suppose there are some known tasks like Carry, Build, etc. A worker can be idle or busy. If they are busy, a task is assigned to them. Which one do you prefer to define task types?
public enum TaskType{
None = 0,
//...
}
TaskType Type;
public enum TaskType{
None = 0,
//...
}
TaskType Type;
public enum TaskType{
Build = 0,
//...
}
TaskType? Type;
public enum TaskType{
Build = 0,
//...
}
TaskType? Type;
Imo, The second one
11 replies
CC#
Created by Gladiator on 12/20/2022 in #help
❔ Exists a node in priority queue or not (O(1)) with value type nodes
In reference types, it is OK. There is an array of a struct type(Element) and I would like to swap some elements in it. Each element keeps its index as well. Outside, an element of that array has been stored in a variable for example. Because it is value type, when swapping elements of the array and update index field, it does not reflect it, what is your idea?
public struct Element{
//...
public int Index;
}
public struct Element{
//...
public int Index;
}
var el = elements[10];
var el = elements[10];
swap element(10) with element(5) and update index field inside. Now, index in el is not updated because it is value type My original situation is priority queue implemented for value types. I keep index of nodes inside nodes. When the positions of those nodes change (heapify up/down), indices change. Now, suppose I have kept a node of the priority queue in a variable. So, the index is wrong and obsolete I want to know if a node exists in a priority queue or not (O(1)) but with value type nodes not ref types and then update the priority value for that node
4 replies
CC#
Created by Gladiator on 12/12/2022 in #help
❔ Generally, how do you implement a readonly struct?
Hi. Generally, how do you implement a readonly struct? I mean define readonly fields or getter? What is the difference? What about defensive copy? Suppose that struct type contains primitive value types like int, float and also other struct types
2 replies
CC#
Created by Gladiator on 12/10/2022 in #help
❔ static fields for struct types
(1)
public static class StructA
{
public static readonly StructA ValueA = new (..) ;
}
public static class StructA
{
public static readonly StructA ValueA = new (..) ;
}
or as Utility class
public static class Utility
{
public static readonly StructA ValueA = new (..) ;
}
public static class Utility
{
public static readonly StructA ValueA = new (..) ;
}
(2)
public static class Utility
{
private static readonly StructA _valueA = new (..) ;
public static StructA ValueA => _valueA;
}
public static class Utility
{
private static readonly StructA _valueA = new (..) ;
public static StructA ValueA => _valueA;
}
The first and second one are the same but I have seen some libs prefer to implement them like (2) (3)
public static class Utility
{
// Initialize every time.
public static StructA ValueA => new(..);
}
public static class Utility
{
// Initialize every time.
public static StructA ValueA => new(..);
}
and what is the benefit of implementation (3)
7 replies
CC#
Created by Gladiator on 12/6/2022 in #help
Best way to Convert a single element to IEnumerable element
What is the best way (performance, memory, GC) to Convert a single element to IEnumerable<Element> elements? T => IEnumerable<T>
return new []{element};
yield return element;
return new []{element};
yield return element;
9 replies
CC#
Created by Gladiator on 12/6/2022 in #help
❔ From which version, we can call non static methods in field declaration time?
I can call non static methods/props of an object in field declaration time. In which C# version, has it been added? Thanks
15 replies
CC#
Created by Gladiator on 12/5/2022 in #help
Is there any reason why comparing struct types with null does not result in compile error?
When comparing struct types with null, it is always false. So, why doesn't the compiler throw compilation error? When and where do we want to compare struct types with null?! never because it is a logical bug
26 replies
CC#
Created by Gladiator on 11/28/2022 in #help
git clean command in bash file
When I run the command below in bash file, I see weird outputs while I can run it perfectly if I go to the project path and execute
$(sudo git -C $PROJECT clean -fd)
$(sudo git -C $PROJECT clean -fd)
What is the problem? appreciated
2 replies
CC#
Created by Gladiator on 11/26/2022 in #help
❔ Can I use slice operator with loop index (remainder operator%)?
I want to slice an array from start to end index but if end index exceeds the array length, loop it and start from index 0 (remainder operator%)
21 replies
CC#
Created by Gladiator on 11/18/2022 in #help
❔ Task.WhenAll does not work without Task.Run inside
Inside a task, _pathFinder.FindPath is not called. returnFirstPath is false (Task.WhenAll) but if I change list.Add(new Task<PathResult>(action)); to list.Add(Task.Run(action));, it works. Another question, I should use Task.Run to be sure it runs in different threads and get results faster?
for (var j = 0; j < roadPoints.Length; j++)
{
var j1 = j;
Func<PathResult> action = () =>
{
var pathResult = _pathFinder.FindPath
(
sourcePoint,
setting.SearchRange,
roadPoints[j1],
CheckTarget,
CheckObstacle,
ComputeWeight
);
return pathResult;
};
list.Add(new Task<PathResult>(action));
}

PathResult pathResult;
if (returnFirstPath)
{
pathResult = await await Task.WhenAny(list);
}
else
{
var pathResults = await Task.WhenAll(list);
pathResult = pathResults.MinBy(p => p.Path.Length);
}
for (var j = 0; j < roadPoints.Length; j++)
{
var j1 = j;
Func<PathResult> action = () =>
{
var pathResult = _pathFinder.FindPath
(
sourcePoint,
setting.SearchRange,
roadPoints[j1],
CheckTarget,
CheckObstacle,
ComputeWeight
);
return pathResult;
};
list.Add(new Task<PathResult>(action));
}

PathResult pathResult;
if (returnFirstPath)
{
pathResult = await await Task.WhenAny(list);
}
else
{
var pathResults = await Task.WhenAll(list);
pathResult = pathResults.MinBy(p => p.Path.Length);
}
95 replies
CC#
Created by Gladiator on 11/18/2022 in #help
Why are Linq methods incomplete? (MinBy, MaxBy, etc.) [Answered]
There is no method to return object by field which is minimum/maximum. I know I can use Aggregate method but it is weird to me Min/Max linq methods does not return the object itself
15 replies
CC#
Created by Gladiator on 11/12/2022 in #help
❔ Separating struct array data
Is it worth separating fields of array data (struct array) into different data structures so that cache hit count increases? Because the struct size reduces and more elements of the array can be cached When I say separation I mean smart separation (relevant data in a struct)
7 replies
CC#
Created by Gladiator on 11/10/2022 in #help
❔ Using concurrent collections to add or remove elements for in-memory collections (server)?
Should we use concurrent collections to add/remove elements to/from in-memory collections in asp.net? For example adding/removing players in online player list
13 replies
CC#
Created by Gladiator on 11/8/2022 in #help
Avoid line break and wrapping (Formatting in rider)
What is the most common way to format codes in rider when a line is long? Break lines or wrapping. Line break can cause awkward weird ugly codes
24 replies
CC#
Created by Gladiator on 11/8/2022 in #help
git push error 502
I get this error when pushing the entire project in new remote repo
git push origin ios/master
Enumerating objects: 30715, done.
Counting objects: 100% (30715/30715), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10920/10920), done.
error: RPC failed; HTTP 502 curl 22 The requested URL returned error: 502
send-pack: unexpected disconnect while reading sideband packet
Writing objects: 100% (30715/30715), 555.24 MiB | 63.00 KiB/s, done.
Total 30715 (delta 22603), reused 27045 (delta 19679), pack-reused 0
fatal: the remote end hung up unexpectedly
Everything up-to-date
git push origin ios/master
Enumerating objects: 30715, done.
Counting objects: 100% (30715/30715), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10920/10920), done.
error: RPC failed; HTTP 502 curl 22 The requested URL returned error: 502
send-pack: unexpected disconnect while reading sideband packet
Writing objects: 100% (30715/30715), 555.24 MiB | 63.00 KiB/s, done.
Total 30715 (delta 22603), reused 27045 (delta 19679), pack-reused 0
fatal: the remote end hung up unexpectedly
Everything up-to-date
I have changed some settings but does not work git config --global http.postBuffer 500M git config --global http.version HTTP/1.1
1 replies
CC#
Created by Gladiator on 9/2/2022 in #help
Equals for structs [Answered]
Is it standard to implement GetHashCode and == and Equals for structs even if all fields should be used? because the default one is using reflection to get fields
6 replies