C
C#4mo ago
MorryMar

a problem in a simple code

ima tryna learn and got a idea of something but it doesnt seem to work does anyone know why?
No description
29 Replies
MorryMar
MorryMar4mo ago
figured it out the math needs to be a double
MorryMar
MorryMar4mo ago
but what is this?
No description
Thinker
Thinker4mo ago
You forgot the parenthesis after Console.ReadLine What it's currently trying to do is convert the method itself to an integer, which it very obviously cannot do. You need to call the method, i.e. Console.ReadLine(). On a side-note, use int.Parse instead of Convert.ToInt32
MorryMar
MorryMar4mo ago
oh thanks man whats the difference?
Thinker
Thinker4mo ago
int.Parse is just kinda... better For a variety of reasons: for one it doesn't allow you to try convert arbitrary objects to integers, and you happen to pass null to it, it throws an exception instead of returning 0.
MorryMar
MorryMar4mo ago
but for converting strings do i still use convert?
Thinker
Thinker4mo ago
No, you use TheTypeYouWantToConvertTo.Parse(yourString), eg. int.Parse(yourString), double.Parse(yourString), etc.
MorryMar
MorryMar4mo ago
it doesnt exist?
No description
MorryMar
MorryMar4mo ago
yea double and int work but string doesnt
Thinker
Thinker4mo ago
If you want to convert something to a string then you just use .ToString() yourVariable.ToString()
MorryMar
MorryMar4mo ago
No description
No description
MorryMar
MorryMar4mo ago
i hate that bug but why does it not say its a string?
Thinker
Thinker4mo ago
So, l.GetType is a method. Methods are kinda weird, because you can both call them, but you can also pass them to other methods as values, just like numbers, string, etc. . When you pass a method as a value to another method, what you're actually passing is something called a Func or an Action, which you don't need to worry about what those actually are for now, but that's what's happening when you call Console.WriteLine(l.GetType) - you're passing l.GetType as a value to Console.WriteLine.
MorryMar
MorryMar4mo ago
hm yea
Thinker
Thinker4mo ago
And when Console.WriteLine tries to display a Func or Action, it prints out something like System.Func`1[System.Type] So in general: if something is a method, you have to call it by putting () after it.
MorryMar
MorryMar4mo ago
oh yea it shows as a sstring now thank u so basically anything that changes something or activate code is called a function and a function always have a () at the end of it for it to be called?
Thinker
Thinker4mo ago
Yep, although in C# a "function" is generally called a "method"
MorryMar
MorryMar4mo ago
hm looks like i have to pass over my small knowledge of lua and python to c# xD
Thinker
Thinker4mo ago
But yeah, functions/methods run other code.
MorryMar
MorryMar4mo ago
ok another question if i may
Thinker
Thinker4mo ago
sure
MorryMar
MorryMar4mo ago
do yk any good playlists for learning c#?
MorryMar
MorryMar4mo ago
Bro Code
YouTube
C# Full Course for free 🎮
C# tutorial beginners full course (C# for Unity) #C# #tutorial #Unity ⭐️Time Stamps⭐️ #1 (00:00:00) C# tutorial for beginners #2 (00:06:30) output 💬 #3 (00:10:48) variables ✖️ #4 (00:19:32) constants π #5 (00:20:35) type casting 💱 #6 (00:27:49) user input ⌨️ #7 (00:31:24) arithmetic operators 🧮 #8 (00:35:54) Math class 📏 #9 (0...
MorryMar
MorryMar4mo ago
after it i wanna learn more
Thinker
Thinker4mo ago
Most people here will generally recommend $helloworld
MorryMar
MorryMar4mo ago
after the course should i go watch helloworld?
Thinker
Thinker4mo ago
sure
MorryMar
MorryMar4mo ago
ok thanks man