C
C#3y ago
ero

add multiple values inside a list

why don't you want to use AddRange?
47 Replies
canton7
canton73y ago
I mean, the answer to "How do I add elements to a list" is "Use Add or AddRange". If you then say "I don't want to do that", the obvious response is "Why don't you want to do that?"
ero
eroOP3y ago
i mean i can guess why, i just want to hear it from them
Pounika
Pounika3y ago
because
canton7
canton73y ago
I mean, the obvious solution to that is not to have 50+ const string members called meaningless things like A24
ero
eroOP3y ago
??????????????????????????????
Pounika
Pounika3y ago
thats the data so youre telling me to rename it all or?
ero
eroOP3y ago
what "data" is that where did it come from
Pounika
Pounika3y ago
theyre object ids bro someone said its easy with a class but i cant find anything about it online i mean they didnt say it was easy they said it could be doe done
canton7
canton73y ago
Do you need all of those as const string members? Can't you just put them in an array to start with?
HimmDawg
HimmDawg3y ago
If you must, you can use list initializers
List<string> list = new List<string>() { "string1", "string2", ... }
List<string> list = new List<string>() { "string1", "string2", ... }
Although I think having all those constants is not very eligant
Pounika
Pounika3y ago
can i put them in an array without writing all their names one by one, then?
canton7
canton73y ago
I think you're missing the point of what I'm saying
ero
eroOP3y ago
ʳᵉᶠˡᵉᶜᵗᶦᵒⁿ
canton7
canton73y ago
I'm saying do:
public static class StringConsts
{
public static readonly string[] AStrings = new[] { "...", "...", "..." };
public static readonly string[] DiStrings = new[] { "...", "...", "..." };
}
public static class StringConsts
{
public static readonly string[] AStrings = new[] { "...", "...", "..." };
public static readonly string[] DiStrings = new[] { "...", "...", "..." };
}
Rather than:
public static class StringConsts
{
public const string A03 = "...";
public const string Di01 = "...";
}
public static class StringConsts
{
public const string A03 = "...";
public const string Di01 = "...";
}
sssh 😉
ero
eroOP3y ago
i think static string[] AStrings => ... is better? there's some weird stuff with that
Jester, your Cat
you dont have to create a new instance every time
ero
eroOP3y ago
and i'm not
canton7
canton73y ago
No? That will create a new array every time it's accessed. Use an ImmutableArray<string> or IReadOnlyList<string> if you want to stop someone re-assigning elements
ero
eroOP3y ago
static is weird like that nope @Zombie help me out
Zombie
Zombie3y ago
You might be thinking of the ReadOnlySpan optimization public static ReadOnlySpan<byte> Stuff => new byte[] { ... }; That won't allocate But it's not specific to static, you can do the same thing in a local etc
ero
eroOP3y ago
man i knew there was something just not what
Jester, your Cat
huh or just do static byte[] Stuff {get;}=new byte[]{...};
Zombie
Zombie3y ago
That's not as efficient
Jester, your Cat
wdym
Zombie
Zombie3y ago
The ReadOnlySpan trick will store the bytes in the .data section of the .dll/.exe And you'll get a span directly over the memory No allocation
ero
eroOP3y ago
forbidden knowledge
Jester, your Cat
oh HmmNoted
Zombie
Zombie3y ago
GitHub
Refer directly to static data when ReadOnlySpan wraps arrays of byt...
Refer directly to static data when ReadOnlySpan wraps strings or arrays of primitive literals. No need to allocate anything in this case. Fixes:#23358 Related:dotnet/corefx#25413 Ask Mode template ...
Zombie
Zombie3y ago
GitHub
[Proposal]: ReadOnlySpan initialization from static data · Issue #5...
ReadOnlySpan initialization from static data Proposed Prototype: Not Started Implementation: Not Started Specification: Not Started Summary Provide a syntax for initializing a ReadOnlySpan&lt;T...
Jester, your Cat
this is just allocating it once and then you can access it with a getter. ofc not as good as that trick but better than =>
ero
eroOP3y ago
why specifically bytes lol why not any unmanaged T
Zombie
Zombie3y ago
because bytes aren't subject to endianness for larger-than-byte types, you need to swap the data if the endianness doesn't match the host
ero
eroOP3y ago
zang
Zombie
Zombie3y ago
RuntimeHelpers.CreateSpan(RuntimeFieldHandle) Method (System.Runtim...
Provides a fast way to access constant data stored in a module as a ReadOnlySpan.
Jester, your Cat
then just do that Clueless
Zombie
Zombie3y ago
So ReadOnlySpan<int> Values => new int[] { ... } is efficient too
ero
eroOP3y ago
this fucking language lmao
Jester, your Cat
c# is a messy bowl of sugar
Zombie
Zombie3y ago
This is less syntax sugar and more of a compiler optimization
Jester, your Cat
there is more than sugar in that bowl
Zombie
Zombie3y ago
If you're assigning an array to a ReadOnlySpan, you can't modify it, so the compiler is allowed to just point the span at constant data And constant data can be stored directly in the executable/library, rather than allocated at runtime So you get it for "free" static byte[] Values { get; } = new byte[] { ... } while still only one allocation, actually does have a cost
Jester, your Cat
writing my whole application with readonly spans only and stackalloc from now on Clueless
Zombie
Zombie3y ago
if you have thousands of those, it will have a significant cost to initialize them all, this was a real problem in TerraFX.Interop.Windows for COM GUIDs they were all eventually changed to use the ROS<T> trick and it eliminated the startup delay involved with allocating the arrays
Jester, your Cat
cool
Jester, your Cat
sometimes i just do my own com interop a little less efficient but nicer to use than that

Did you find this page helpful?