C
C#10mo 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
MorryMarOP10mo ago
figured it out the math needs to be a double
MorryMar
MorryMarOP10mo ago
but what is this?
No description
Thinker
Thinker10mo 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
MorryMarOP10mo ago
oh thanks man whats the difference?
Thinker
Thinker10mo 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
MorryMarOP10mo ago
but for converting strings do i still use convert?
Thinker
Thinker10mo ago
No, you use TheTypeYouWantToConvertTo.Parse(yourString), eg. int.Parse(yourString), double.Parse(yourString), etc.
MorryMar
MorryMarOP10mo ago
it doesnt exist?
No description
MorryMar
MorryMarOP10mo ago
yea double and int work but string doesnt
Thinker
Thinker10mo ago
If you want to convert something to a string then you just use .ToString() yourVariable.ToString()
MorryMar
MorryMarOP10mo ago
No description
No description
MorryMar
MorryMarOP10mo ago
i hate that bug but why does it not say its a string?
Thinker
Thinker10mo 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
MorryMarOP10mo ago
hm yea
Thinker
Thinker10mo 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
MorryMarOP10mo 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
Thinker10mo ago
Yep, although in C# a "function" is generally called a "method"
MorryMar
MorryMarOP10mo ago
hm looks like i have to pass over my small knowledge of lua and python to c# xD
Thinker
Thinker10mo ago
But yeah, functions/methods run other code.
MorryMar
MorryMarOP10mo ago
ok another question if i may
Thinker
Thinker10mo ago
sure
MorryMar
MorryMarOP10mo ago
do yk any good playlists for learning c#?
MorryMar
MorryMarOP10mo 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
MorryMarOP10mo ago
after it i wanna learn more
Thinker
Thinker10mo ago
Most people here will generally recommend $helloworld
MorryMar
MorryMarOP10mo ago
after the course should i go watch helloworld?
Thinker
Thinker10mo ago
sure
MorryMar
MorryMarOP10mo ago
ok thanks man
Want results from more Discord servers?
Add your server