rgument 2: cannot convert from 'int' to 'System.Func<int, decimal>'
Error (the title) is when I do
nums.Sum(lineReading); idk why31 Replies
in short, the user has a "menu" where they can choose which
.txt file to open. Then they get the option what to do with it. When they press "1" it will read the file, and add up all the numbers inside of that file and then do the sum with the numbers it has
every number is seperated on each line in the files to make it easier to read. Thats why there is no Split()Why are you passing an argument to Sum() ?
nums.Sum() takes a lambda, not a value you're tyring to do [1, 2, 3].Sum(4), which is non-sensical.You need to assign the value of
nums.Sum() (no argument) to a variable.also,
nums.Sum() does not change anything, it returns the sum calculated. so unless you print it (Console.WriteLine()) or store it (var x = ), it's uselessoh yea pffft 🤦♂️ sorry i been making exercises for a few hours now
yea this is only a snippet of the code
for the rest does it look ok
it looks like it works. it's not how i would write it, but i'm not one to judge...
still learning bro 😭
inb4 someone says don't use
Convert.ToInt32i'd do something like:
looks clean but looks like chinese to me 😭
BTW if you want to use StreamReader you can pass it the file name directly in the constructor. You don't need to first open a raw FileStream and then pass that in.
So you can simplify that code a bit.
wait whaaaaat
also, yo udon't have to use
{} with using
but File.ReadAllLines() gives you an array with every line of the file, so yo udon't even hve to mess with readers or while loopsdamn...
then why they teaching it to us like this 😭
Learning how to use streaming APIs is useful because sometimes you don't want to read the entire thing into memory.
For instance if the file is gigabytes in size.
oh ok
then this can be shortened to the version i gave you above by learning about LINQ and
.Select()Another small thing you can fix if you want. The idiomatic way to stream through an entire text file is:
That way you get the line in one shot and don't need to use EndOfStream.
yup. useful to knwo both streamreader and readalllines.
so here
sr.ReadLine(); assigns it to the variable line (if its a valid string of course)and if it's not
nullYes, this is using a very neat feature of C# called pattern matching.
When you pattern match on the result of a method call (using
is in this case) you can assign it to a variable at the same time (in this case line) if it matches the pattern.
It's very nice for this kind of thing.yea super chill
If you want to learn more about pattern matching, @viceroypumpkin | 🦋🎃 did a very nice talk on it 🙂
https://youtu.be/w3m-THz--6A
.NET Foundation
YouTube
Solution2: Adventures in Pattern Matching with Stuart Turner
An in-depth guide to migrating existing code to use pattern matching!
Join us on Discord: https://discord.com/invite/csharp
JetBrains: https://www.jetbrains.com/
O'Reilly: https://www.oreilly.com
appreciate it
thats u in the vid? @viceroypumpkin | 🦋🎃
aye, yes
nice hat