How do i get this to work?
error: Cannot implicitly convert type 'bool' to 'string'
i clicked on the error and it told me to go here
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0029?f1url=%3FappId%3Droslyn%26k%3Dk(CS0029)
here i see are only ints being converted to long is what im doing even possible?
Compiler Error CS0029 - C# reference
Compiler Error CS0029
57 Replies
Let's try to make this as simple as possible.
A method can return something or nothing.
ReadLine
is a method from the Console
class.
As you can see in this image, this method in particular will return something.
This is from the docs.
So we know ReadLine will return a string.
So obviously u cannot do.
because int is not a string.
Now let's go a bit more complex...
Methods can have parameters that you pass into it.

as you can see the
TryParse
method have 2 parameters a string
and an int
and it also returns a bool
so we know that ReadLine
gives us a string right?
so we can give ReadLine
to TryParse
because it will provide it with a string
and we know that the second parameter will output a result of type int
we can later use.
and we also know that it will return a bool
.
i.e.:
so when this line executes, it will go into the Console.ReadLine()
and ask the user for an input and pass it to the method.
which will then evaluate if the inpu is true or false and store it to result
if it was successful value will have the actual value, if it failed value will have a default value give nto it, in the case of an int that value is 0.
that is why its important to use a condition before you use the value so you dont mistakenly use it when its invalidgive me one sec to read this thanks for the reply!
Q1. When they say 'Returns the next line of character stream' by stream would that be my input? like what i put into the method via the cmd pop up?
q2.i notice that it returns (by returns i mean converts a string into an int) a int value how would it
q4 to test wether bool result = int.TryParse(Console.ReadLine(), out int value);
returned true or false i just added console.writeline(result) to the code and i got it to print true or false is this a way to check the LOC it self to see if thats what it does is this a standard pratice of some type of checking system?
sorry for late reply had to re read a few times
q1 anything typed until u hit enter
q2 can you be more specific here as to which method and location are u referring
q4 no that is not checking anything, that is just outputting the value it holds
checking would be using a condition to properly evaluate whether u should go to path A or B
i.e.:
Here we are checking if result goes to path A or B and acting as needed.
q1 clear! ty
q2 i just read my question and idk even know why i typed that?
q4 when you ouptting the value it holds i notice that if i enter the letter e its false if i enter the number 4 its true is what i mean
yes the idea of the int.TryParse method is that it wants to verify all the characters typed are in fact digits that can be used to compose a whole number
okay ty bud im gonna be re reading this chat today and tomrrow! ty!
just practice types and if ur using visual studio $typehints
Visual Studio 2019 and above can now give you inline hints of the type in lambda, var, etc, see the animated GIF on how to enable it:
https://cdn.discordapp.com/attachments/169726586931773440/869498806343962694/2021-07-27_05-35-23.gif
See the below image for an example of what it looks like the white line was made by me to illustrate what the above does:
https://media.discordapp.net/attachments/569261465463160900/894398422365372416/unknown.png
NOTE: You can also use it by pressing ALT + F1! You might need to go on the settings and disable/reenable it for it to work!
it helps displaying what the type is so it might make things clearer for u
i think i turned them off but i'lll re enable
mines a bit different?

scroll down?

im off to bed but its there anyway just search the name at the top
.
the way to tell if something returns something is if it contains parenthesis ?
no, its by looking at the type before the method name
usually the parenthesis indicates whether the method has or not parameters
if its empty () then it has no parameters in the case of ReadLine for example
TryParse for example has multiple parameters
so what comes before the method name is what define what type its returning or if its returning nothing aka void
Methods - C#
A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.

are all data types return types? @leowest
"return types" are not sume super special subset of "types"
It's just a type that a method returns
int Foo()
has int
as the return type
Person Bar()
has Person
as the return type
(int, string)[] Baz()
has an array of (int, string)
tuples as the return type
And so onso public static string? ReadLine();
has a return type of string
Of
string?
Which is "string
or null
"the return types would be string or null ?
The return type would be
string?
would it also be null?
What do you mean?
uh
Yes,
string?
can be a string
, and can also be null
and string and null both be returned types?
The return type of that method is
string?
Which means the method can return a string
or a null
So, in that way, I guess, yeahso the return type is string? but the VALUES it can return are either a string or a null value?
Yes
okay! 😮
It would be clearer if C# had union types, the return would be
string|null
thenso in the return type is bool ?
Makes it much more obvious than a random
?
But it is what it is
Yesgonna put this in my notes!
im getting return confused with something else
when i run this code:
and i typed something like Add(1,4); would the value of 1,4 be a 'Return' is that the word we use?
Not sure what you mean?
Add(1, 4)
would return 5
uh
when passing 1,4 and having it SPIT OUT 5 be called a return type or a return ? are they the same thing ?
5
would be the return value, I guess?
Return type is int
okay!

its a combination
they have to match to work
what u put in return must match with the signature u have in your method
so if u have int u can't return double or string
'return type' is the type of value from the list of types lets say (int,string,float,bool,char) ...? ff ive lost my self
so return type = the value that gets spitted out right?
return type is the type that you must output when returning a value if anything
leowest
REPL Result: Failure
Exception: CompilationErrorException
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 275.068ms | Execution: 0.000ms | React with ❌ to remove this embed.
as you can see the addition there would result in a double so it cannot return the double, because it wants u to provide it with an int
leowest
REPL Result: Failure
Exception: CompilationErrorException
Compile: 228.849ms | Execution: 0.000ms | React with ❌ to remove this embed.
the type u define in the method signature is the rule for what it will return to whoever call that method
this is a side question buy both public and static are access modifiers?
`cs
public static
public
yes, static
noty !
@Angius is there a difference between an access modifer anda modifer ?
Uh, like squares and rectangles, I guess?
An access modifier is a modifier
A modifier is not necessarily an access modifier
oh