code explaination
hi im new and i would really like someone more advanced to explain what this code does and also answer up my following up questions, thanks :D
83 Replies
for example this one, is there any way i could make the variable binary an integer but still having the 101010 when i WriteLine it?
same with hexn can i have it stored in an int variable?
Angius
REPL Result: Success
Console Output
Compile: 444.430ms | Execution: 29.194ms | React with ❌ to remove this embed.
0b
prefix
0o
for octal, 0x
for hexadecimalsince when was octal added? also how to convert from binary to hexadecimal and store it in one variable like int hexNumber
and
.ToString("B")
will turn the number into a binary string in the most recent versions of .NETInteger is integer
Hex, oct, dec, bin
No matter
It's the same value
yeah i know but like uhhh i want it to display by default in the given number system
That
:X
displays in hex
:B
would display in bin, IIRCthe default is decimal, any other format you need the format specifier
Standard numeric format strings - .NET
In this article, learn to use standard numeric format strings to format common numeric types into text representations in .NET.
Angius
REPL Result: Success
Console Output
Compile: 415.012ms | Execution: 70.977ms | React with ❌ to remove this embed.
okay thanks i think i understand that
okay can someone explain how does substring work?
like lets say that my string is WillSmith
Angius
REPL Result: Success
Result: string
Compile: 228.866ms | Execution: 17.747ms | React with ❌ to remove this embed.
(start_index, length)
thats all?
ye
oh that seems easy
Also
yeah
bonus points if its ranges
Angius
REPL Result: Success
Result: string
Compile: 206.900ms | Execution: 26.016ms | React with ❌ to remove this embed.
This takes all letters from index 1 (inclusive) to index 5 (exclusive)
i only had the .Substring thing
on my lessons
A substring also works
Ranges and indices were added merely a few years ago, the academia did not have the time to catch up yet :KEKW:
i think that my teacher knows that but he didnt want to overwhelm us like we are doing more than we should be already as he said
Yeah, makes sense
its my 3rd month of learning from the scratch never touched c# before
can i change the separators1 to something else inside the .Split ?
like
so that instead of the name separators1 i have them listed
Like, directly there, without a variable?
Angius
REPL Result: Success
Result: string[]
Compile: 288.905ms | Execution: 96.755ms | React with ❌ to remove this embed.
This works
so the code will be like this?
yup
And the
separators1
line can be deleted nowyeah alright alright
can you explain how this throw new works? @Angius
It throws the given exception
wdym by throws
Causes an error to happen
is the throw connected to try and catch in my main segment of the code
Yes
where i have catch(ArgumentOutOfRangeException
Thrown exceptions can be caught
so if i throw the exception FormatException
and inside of my catch of formatexception i have console.writeline("bad error")
"bad error" will appear on my screen
?
Yep
Angius
REPL Result: Success
Console Output
Compile: 397.044ms | Execution: 24.897ms | React with ❌ to remove this embed.
okay i think i understand it now
also when i have colors of the console text
for example red
how do i make it
?
and how to transfer it into a function
for example a function DisplayMessage
that gets a string (what to display) and color of the text
also my teacher told me that there were some exception order? like from the most describing to the least could you sort it out so that it gives me more specific outputs if it happens? list: argumentoutofrangeexception, formatexception, exception, dividebyzeroexception, invalidoperationexception
@Angius
Regarding the first one, set
Console.ForegroundColor
Regarding exceptions, I think it's about inheritance? All exceptions, generally, inherit from Exception
. Then, other exceptions can inherit those
If you catch Exception
, you will catch any exception
If you catch FormatException
, you will catch only that, and all that inherit from itmy teacher made it so that there were some special ones that had different attributes such as the console color and then the general one when an unknown error occured if u understand what i mean
also how does this work? it outputs
Number cannot be less than zero (Parameter 'a')
okay so which one is the most specific and which one is the least?Well, since
Exception
is the base of them all, it will be the least specific
For the others, look them up in the documentation, see what they inherit fromokay what about the img?
ArgumentOutOfRangeException
override's Exception
's Message
property, and formats it in the way that you see
It also has a constructor that takes multiple parametersHere's the
string, string
one you're usingi dont understand everything in it
like your img
No need to understand everything
I'm just showing you that exceptions can have whatever code inside of them you want
They can have constructors with multiple parameters and everything
And they can format the message however you want
ohhhhh okay
btw do i take the color with string into the function?
or is there any other variable
Not sure what you mean
Yes, you can move the coloring code and stuff into a separate function
yes but like
is it
static void DisplayMessage(string message, string color)
$tias
You're making this method
You decide
idk how to write it sorry
thats what came up
What do you mean "came up"?
alt + enter
to create the private static void
Right
Well
First and foremost, that's not how you call methods
You don't write
Console.WriteLine(string "hello world")
so why is it DisplayMessage(string message)
?huh?
DisplayMessage(message, color)
okay now it works kinda wait
Yeah
Except
Console.ForegroundColor
is not of type string
It's of type ConsoleColor
oh
okay
it works now :D
Nice
You could also just use it with
oh
makes sense
yeah
thanks
i guess no more questions