C
C#ā€¢8mo ago
Ace Guy

ā” Input into an int array

The input would be something like "1 2 3 4 5", how could I convert it into an int array/several variables?
140 Replies
Pobiega
Pobiegaā€¢8mo ago
Think about it. You have a single string that contains all your values. There is a method that takes a string and parses it into a single int. We cant use that here, as it would be confused if you were to do this in pseudocode, just describing the steps, what would you do?
Ace Guy
Ace Guyā€¢8mo ago
hmm probably convert the string char by char until a space, then move to the next variable/array slot hm this is probably doable with a cycle, but is there any better method?
Pobiega
Pobiegaā€¢8mo ago
Well, there is a built in function to split one string into many by giving it a pattern to look for
Ace Guy
Ace Guyā€¢8mo ago
what is it?
Pobiega
Pobiegaā€¢8mo ago
string.Split you can use it on your string directly yourInputVar.Split(...)
Ace Guy
Ace Guyā€¢8mo ago
oh I think this is exactly what I was looking for, thanks!
Pobiega
Pobiegaā€¢8mo ago
almost šŸ™‚ it gives you a string[] back so you still need to do something to that to turn it into an int[]
Ace Guy
Ace Guyā€¢8mo ago
found Array.ConvertAll on google and it works perfectly, ty for help
Pobiega
Pobiegaā€¢8mo ago
that works, I guess. Not what I'd have gone with personally
Ace Guy
Ace Guyā€¢8mo ago
What would you do?
Pobiega
Pobiegaā€¢8mo ago
s.Split(' ').Select(x => int.Parse(x)).ToArray();
Ace Guy
Ace Guyā€¢8mo ago
huh, intresting tysm again
Exeteres
Exeteresā€¢8mo ago
Or even simpler: s.Split(' ').Select(int.Parse).ToArray();
joren
jorenā€¢8mo ago
It's not considered bad practice using those extension methods in non-LINQ code? I know they are part of Enumerable and not necessarily LINQ limited, didnt see them very often until LINQ though. (sorry OP if im bloating your post)
Pobiega
Pobiegaā€¢8mo ago
wdym? why would it be bad to use LINQ?
joren
jorenā€¢8mo ago
I mean I dont know, I dont think its bad. Not sure if thats normal
x0rld
x0rldā€¢8mo ago
you mean use linq on an array not a list ?
Pobiega
Pobiegaā€¢8mo ago
I wouldn't do it in my 144 fps games hot loop, but in 99% of normal code its not only normal, its encouraged
joren
jorenā€¢8mo ago
I mean an Array would be deriving from IEnumerable, so it being an array doesnt matter
Pobiega
Pobiegaā€¢8mo ago
it does, yes
joren
jorenā€¢8mo ago
I mean okay, were these extension methods initially meant for LINQ but OK to be utilized outside of LINQ queries?
Pobiega
Pobiegaā€¢8mo ago
LINQ leads to short, readable, understandable code, at the cost of a tiny tiny performance degradation (over manual loops) uhm what do you think LINQ is? this IS a linq query
joren
jorenā€¢8mo ago
Yes, since you it a linq query by using the extension methods
x0rld
x0rldā€¢8mo ago
you don't have to write the sql like things to use linq
Pobiega
Pobiegaā€¢8mo ago
I just dont understand your question, in either of your sentences
I know they are part of Enumerable and not necessarily LINQ limited
.Select and .ToArray are both LINQ methods but what is "non-LINQ code" and "LINQ code"? thats... not a distinction LINQ is just a collection of extension methods that work on most if not all collections, thanks to IEnumerable
joren
jorenā€¢8mo ago
string inputStr = "1 2 3 4 5";
string[] strValues = inputStr.Split(' ');
int[] intArray = Array.ConvertAll(strValues, int.Parse);
string inputStr = "1 2 3 4 5";
string[] strValues = inputStr.Split(' ');
int[] intArray = Array.ConvertAll(strValues, int.Parse);
If i am not wrong this doesnt utilize any of the LINQ extension methods and achieves the same, this is what i'd refer to as "non-LINQ" code, simply not using LINQ extension methods and therefore not creating a query
Pobiega
Pobiegaā€¢8mo ago
hm, wasn't aware that Converter<TIn, TOut> had an implicit operator for Func<TIn,TOut> but yeah, this would do exactly the same thing
joren
jorenā€¢8mo ago
the whole concept of just creating a "query" to put values into an array seems so forgein to me ig seems wrong
Pobiega
Pobiegaā€¢8mo ago
.. a query? there is no query
joren
jorenā€¢8mo ago
not in this case
Pobiega
Pobiegaā€¢8mo ago
there is a lazy select iterator
joren
jorenā€¢8mo ago
I mean in the examples given above by you and the xtreit
Pobiega
Pobiegaā€¢8mo ago
there is no query in those either, by most definitions of query
joren
jorenā€¢8mo ago
so utitlizing LINQ extension methods != it being a query?
Pobiega
Pobiegaā€¢8mo ago
also, can we even use the term query without a context? like, how do you define a query
joren
jorenā€¢8mo ago
like, a question or request i suppose in english that is when talking about SQL-context its more request
Pobiega
Pobiegaā€¢8mo ago
but there is no SQL involved here, no databases its just an array.
joren
jorenā€¢8mo ago
yeah exactly you said it was a query though previously this
Pobiega
Pobiegaā€¢8mo ago
well, I used that term because you did because technically its true if you refer to an IEnumerable iterator as a query like, thats fine, its fairly common to do so but there is no... request being made here
joren
jorenā€¢8mo ago
not to a data source like SQL, XML, etc no so I wouldnt call it a query then personally
Pobiega
Pobiegaā€¢8mo ago
okay, then there are no queries šŸ™‚ its just a method being called once for each item in an array
joren
jorenā€¢8mo ago
either way, so no harm in using these LINQ extensions anywhere I wish unless I want performance
Exeteres
Exeteresā€¢8mo ago
linq queries are much lighter and simpler than they seem for example, ToArray takes into account the size of the source array to allocate the space it needs right away there are plenty of such optimizations there I don't see any reason to use any more "specific" methods for working with collections
No description
No description
Pobiega
Pobiegaā€¢8mo ago
yeah, LINQ is incredibly well written and uses some freaky magic in the background to make it faster it has a tiny overhead compared to self-made for loops tiiiny
joren
jorenā€¢8mo ago
Okay, seems like LINQ extension methods should be considered universal and utilized for both actual queries (from a DB for instance) and normal code. mhm
Pobiega
Pobiegaā€¢8mo ago
yep!
joren
jorenā€¢8mo ago
I'll keep that in mind
Pobiega
Pobiegaā€¢8mo ago
and actually, when used against a DB via something like EFCore its not actually LINQ šŸ˜„
joren
jorenā€¢8mo ago
though if I want performance I prob wouldn't opt for C# due to my lack of knowledge and inability to optimize as much as I could in C++
Pobiega
Pobiegaā€¢8mo ago
they just have the same methods that behave the same way, but its on IQueryable and uses expressions to compile into SQL
joren
jorenā€¢8mo ago
maybe that changes after I have a better grasp on C# makes sense, thanks
Pobiega
Pobiegaā€¢8mo ago
yeah depends on the usecase tbh. C# can be very fast, if written well - but ultimately its a garbage collected language if you want to reach those blazingly fast perf numbers, you need C/CPP/Rust/Zig etc
joren
jorenā€¢8mo ago
at the cost of your sanity šŸ˜‰
Pobiega
Pobiegaā€¢8mo ago
yeah, and readability šŸ˜„
joren
jorenā€¢8mo ago
Ill make a new post about some async clarification, thanks for the help and sorry OP for bloating your topic šŸ˜‰
Pobiega
Pobiegaā€¢8mo ago
thats what I like about C#, its very fast for what it is but still maintains good readability
joren
jorenā€¢8mo ago
yeah, though I dont like some of it like the
void func() => 1 + 1
void func() => 1 + 1
it looks so odd in a class, especially for people that just started with C# its weird
Pobiega
Pobiegaā€¢8mo ago
expression bodies? they are ā¤ļø
joren
jorenā€¢8mo ago
they are fire ye, theyre nice syntax sugar to have
Pobiega
Pobiegaā€¢8mo ago
but yeah, there is... a lot to take in for someone new to the language
joren
jorenā€¢8mo ago
but its such an odd thing to see for the first time I also dont like Nullable types
Pobiega
Pobiegaā€¢8mo ago
O_? heathen
joren
jorenā€¢8mo ago
the ?, id prefer Nullable<T> being the only option so its more clear
Ace Guy
Ace Guyā€¢8mo ago
I'm preparing for a programming contest so hopefully they allow LINQ lol
joren
jorenā€¢8mo ago
I shared a non LINQ solution to that one problem :D
Ace Guy
Ace Guyā€¢8mo ago
saved that too, ty btw
Pobiega
Pobiegaā€¢8mo ago
wouldn't be backwards compatible, which ruled that out quickly.
joren
jorenā€¢8mo ago
np bro
Exeteres
Exeteresā€¢8mo ago
we are forced to use this syntax since there are no difference for nullable reference types and non-nullable reference types for CLR all null checks are just compiler checks, nothing else expect some metadata provided by attributes
joren
jorenā€¢8mo ago
forced to use the ? syntax?
Exeteres
Exeteresā€¢8mo ago
yes because it makes sense only for compiler
Pobiega
Pobiegaā€¢8mo ago
remember, the alternative to ? was not having it at all
Exeteres
Exeteresā€¢8mo ago
while Nullable<T> is a generic type
Pobiega
Pobiegaā€¢8mo ago
ie, all reference types being nullable
joren
jorenā€¢8mo ago
I mean doesnt ? yield Nullable<T> to the compiler?
Exeteres
Exeteresā€¢8mo ago
no only for value types
joren
jorenā€¢8mo ago
thats so illegal what T cannot handle reference types?
Exeteres
Exeteresā€¢8mo ago
because all reference types in CLR are nullable historically
Pobiega
Pobiegaā€¢8mo ago
reference types are already nullable
joren
jorenā€¢8mo ago
ah
Pobiega
Pobiegaā€¢8mo ago
they always are
joren
jorenā€¢8mo ago
wtf thats bad
Exeteres
Exeteresā€¢8mo ago
yeah
Pobiega
Pobiegaā€¢8mo ago
by definition
Exeteres
Exeteresā€¢8mo ago
legacy
Pobiega
Pobiegaā€¢8mo ago
a reference can point to null, thus they are nullable
joren
jorenā€¢8mo ago
backwards compatibility is a blessing tot he industry and a bottleneck to a programming's development team
Pobiega
Pobiegaā€¢8mo ago
sry, its not, its a wrapped struct very yes imagine being able to remove null as a concept in modern C# ā¤ļø
joren
jorenā€¢8mo ago
class A
{}

A a = new A();
a = null; // legal?
class A
{}

A a = new A();
a = null; // legal?
Pobiega
Pobiegaā€¢8mo ago
legal, but you will get a warning in modern C# because nullability checks are on by default
Pobiega
Pobiegaā€¢8mo ago
No description
Exeteres
Exeteresā€¢8mo ago
in some modern languages like Dart 3 there is something called "sound null safety" but they had to rewrite almost all their libraries from Dart 2 to bring this feature C# is much older and it will be impossible + reflection so, all reference types will be nullable in the near future
joren
jorenā€¢8mo ago
Interesting, so they solved this issue by introducing ? that would be universal to both reference and value types I really dislike the whole value types ideology either way, I prefer just specifying when something is a reference on any types and not having a set default for a type What's the upside to having certain types be a reference type, by default? but lets say you do: Nullable<ClassType> obj, that'd be considered redundant then no? it's already implicitly nullable?
Exeteres
Exeteresā€¢8mo ago
it is not legal
Pobiega
Pobiegaā€¢8mo ago
you can't, Nullable<T> is a struct and has a where T: struct requirement
Exeteres
Exeteresā€¢8mo ago
since Nullable constrained to value types
joren
jorenā€¢8mo ago
oh they made it illegal, thats rough so when you do ClassType? obj it just omits the ? since its already nullable?
Exeteres
Exeteresā€¢8mo ago
yes exactly and emits some attributes with metadata about nullability on properties, fields, parameters, etc... where this type was used
joren
jorenā€¢8mo ago
that is nasty, thats why no one writes Nullable<T>, you'd create inconsisent code style between reference and value type so you just opt for ? and let the compiler handle it for you
Exeteres
Exeteresā€¢8mo ago
yes
joren
jorenā€¢8mo ago
yikers, thats sad. I find ? pretty ugly sadly oh well makes sense, thanks you two!
Exeteres
Exeteresā€¢8mo ago
can you imagine the use of interfaces (or, in common, abstract types) if any type can be both: value and reference just depending on how it is used?
joren
jorenā€¢8mo ago
I dont see how that'd implicate things honestly I mean C++ has abstract classes, I can create instances of its child classes and they are value types by default I can create a reference to that value type, an alias so to say its dangerous because you can have a dangling reference, much like an invalidated pointer I guess that's where it differs, these types are inherently a certain types, e.g value type or reference type
Exeteres
Exeteresā€¢8mo ago
the main idea is that any reference type is always represented in memory as a reference with fixed size and it allows to consider them more abstract regardless their actual types we can use interfaces (or abstract types) as properties, fields, parameters and we don't care about the actual types and their sizes in the memory
joren
jorenā€¢8mo ago
so what ure saying is that when you create a class instance it creates the instance on the heap (I supposed, since we are using new) on the stack we got a reference to the object and that we pass/use but the actual object isnt directly accessed by us, the programmer?
Exeteres
Exeteresā€¢8mo ago
yep
joren
jorenā€¢8mo ago
bruh so its just heap allocation with pointers, conceptually, but applied with references which is good in C# since we dont want them to manage memory anyway, we got a garbage collector I guess its a good solution since you dont want the programmer to work with pointers in C#, unless its unsafe code block.
Exeteres
Exeteresā€¢8mo ago
yes but there are also a value types here to reduce the pressure on GC in places where it makes sense simply there are primitives like numbers and bools
joren
jorenā€¢8mo ago
are value types always on the stack, or is that up to the compiler?
Exeteres
Exeteresā€¢8mo ago
for example in python all numbers are placed in heap lol
joren
jorenā€¢8mo ago
depending on its size etc I suppose it'd always be on the stack
Exeteres
Exeteresā€¢8mo ago
always
joren
jorenā€¢8mo ago
ye, otherwise the distinction between ref and value types would make no sense and serve no purpose can I manually allocate it on the heap, a value type, lets say a struct
Exeteres
Exeteresā€¢8mo ago
you can box it
joren
jorenā€¢8mo ago
oh right i forgot about that
Exeteres
Exeteresā€¢8mo ago
or place as a field of the class (reference type) "wrap it"
joren
jorenā€¢8mo ago
int a = 4;
object b = a;
int a = 4;
object b = a;
x0rld
x0rldā€¢8mo ago
boxing powa
joren
jorenā€¢8mo ago
b would be a reference type, it references the heap obj thats now on the heap
Exeteres
Exeteresā€¢8mo ago
yep
joren
jorenā€¢8mo ago
it doesnt invalidate a?
Exeteres
Exeteresā€¢8mo ago
no
joren
jorenā€¢8mo ago
you have move semantics?
Exeteres
Exeteresā€¢8mo ago
a is still on the stack yes
joren
jorenā€¢8mo ago
it just copies?
Exeteres
Exeteresā€¢8mo ago
yes it is not rust šŸ™‚
joren
jorenā€¢8mo ago
can I std::move the a to another stack object for instance
Exeteres
Exeteresā€¢8mo ago
you can use ref i think
joren
jorenā€¢8mo ago
which would invalidate a since its moved to b
Exeteres
Exeteresā€¢8mo ago
in parameters
joren
jorenā€¢8mo ago
would that invalidate the original object?
Exeteres
Exeteresā€¢8mo ago
but it will not invalidate something there are no such a concept in C#
joren
jorenā€¢8mo ago
i see makes sense it might just utilize smth like construct in place that avoids unneeded copies then I suppose
int a;

func(ref a); // catch as ref or pass as, or both?
int a;

func(ref a); // catch as ref or pass as, or both?
wouldnt this put it on the heap? basically create an object in the heap and pass the reference?
Exeteres
Exeteresā€¢8mo ago
no it will pass the reference to the stack where a is located
joren
jorenā€¢8mo ago
makes sense otherwise it would suck lol
Exeteres
Exeteresā€¢8mo ago
sure
joren
jorenā€¢8mo ago
heap allocation is expensive is there a big difference between stack and heap allocation in C#
Exeteres
Exeteresā€¢8mo ago
it is the only reason while ref exists in order not to box the value to just transfer it to the calling method
joren
jorenā€¢8mo ago
yeah, just allow references to value types and ure good to go not bad actually not so disappointed anymore in C#'s choices
Accord
Accordā€¢8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
ā” PopupModal dialog not fadeout or fadeinPopup Modal not fadeout when click close button, or when open from menu PopupModal.cs ```cs using Kaāœ… Async app initialization failsHi, I tried to upgrade my app initialization steps by making them async, but I am stuck with errors ā” save checkbox value after refreshIs there a way in Blazor where I save the checkbox after I refresh the page, for example I press a cā” Using Microsoft Graph to verify users exist based on IEnumerable<Guid>I am trying to work with Microsoft Graph to verify that each guid in some list is associated with a āœ… How to register a onNavigate/navigation event callback in Net8 SSR?blazor.web.js replaced the entire html on navigate with the response, but scripts like `<script src=ā” Establishing a connection to SQL Server in Mac (Azure data studio & Docker desktop)Ive been stuck on this error: A network-related or instance-specific error occurred while establishāœ… Reading matricies to an output fileI am trying to read the data inside two matricies from an input file firstly in order to add them toā” How to Deploy WASM Project with .NET Backend in same repo?I have a Blazor frontend with a .NET backend. How do I use Azure to deploy these when they are in thā” EF Core LINQ translation documentIn the process of upgrading a .NET Core 1.1 to 6 app, our EF Core LINQ queries are now hitting some Is there a better (or more appropriate way) of implementing method overloading for my usecase here?I have two methods of a class I'm writing - in one usecase I want to update the field with no return