C
C#3w ago
mich4s

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
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
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?
MODiX
MODiX3w ago
Angius
REPL Result: Success
int binary = 0b101010;
Console.WriteLine($"Number is {binary:X}");
int binary = 0b101010;
Console.WriteLine($"Number is {binary:X}");
Console Output
Number is 2A
Number is 2A
Compile: 444.430ms | Execution: 29.194ms | React with ❌ to remove this embed.
Angius
Angius3w ago
0b prefix 0o for octal, 0x for hexadecimal
mich4s
mich4sOP3w ago
since when was octal added? also how to convert from binary to hexadecimal and store it in one variable like int hexNumber
Jimmacle
Jimmacle3w ago
and .ToString("B") will turn the number into a binary string in the most recent versions of .NET
Angius
Angius3w ago
Integer is integer Hex, oct, dec, bin No matter It's the same value
mich4s
mich4sOP3w ago
yeah i know but like uhhh i want it to display by default in the given number system
Angius
Angius3w ago
That :X displays in hex :B would display in bin, IIRC
Jimmacle
Jimmacle3w ago
the default is decimal, any other format you need the format specifier
Jimmacle
Jimmacle3w ago
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.
MODiX
MODiX3w ago
Angius
REPL Result: Success
int binary = 0b101010;
Console.WriteLine($"Hex {binary:X}");
Console.WriteLine($"Dec {binary}");
Console.WriteLine($"Bin {binary:B}");
int binary = 0b101010;
Console.WriteLine($"Hex {binary:X}");
Console.WriteLine($"Dec {binary}");
Console.WriteLine($"Bin {binary:B}");
Console Output
Hex 2A
Dec 42
Bin 101010
Hex 2A
Dec 42
Bin 101010
Compile: 415.012ms | Execution: 70.977ms | React with ❌ to remove this embed.
mich4s
mich4sOP3w ago
okay thanks i think i understand that okay can someone explain how does substring work? like lets say that my string is WillSmith
MODiX
MODiX3w ago
Angius
REPL Result: Success
"123456789".Substring(2, 3)
"123456789".Substring(2, 3)
Result: string
345
345
Compile: 228.866ms | Execution: 17.747ms | React with ❌ to remove this embed.
Angius
Angius3w ago
(start_index, length)
mich4s
mich4sOP3w ago
thats all?
Angius
Angius3w ago
ye
mich4s
mich4sOP3w ago
oh that seems easy
Angius
Angius3w ago
Also
mich4s
mich4sOP3w ago
yeah
leowest
leowest3w ago
bonus points if its ranges
MODiX
MODiX3w ago
Angius
REPL Result: Success
"1234567890"[1..5]
"1234567890"[1..5]
Result: string
2345
2345
Compile: 206.900ms | Execution: 26.016ms | React with ❌ to remove this embed.
Angius
Angius3w ago
This takes all letters from index 1 (inclusive) to index 5 (exclusive)
mich4s
mich4sOP3w ago
i only had the .Substring thing on my lessons
Angius
Angius3w ago
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:
mich4s
mich4sOP3w ago
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
Angius
Angius3w ago
Yeah, makes sense
mich4s
mich4sOP3w ago
its my 3rd month of learning from the scratch never touched c# before
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
can i change the separators1 to something else inside the .Split ? like so that instead of the name separators1 i have them listed
Angius
Angius3w ago
Like, directly there, without a variable?
MODiX
MODiX3w ago
Angius
REPL Result: Success
"a,b,c;d".Split([',', ';'])
"a,b,c;d".Split([',', ';'])
Result: string[]
[
"a",
"b",
"c",
"d"
]
[
"a",
"b",
"c",
"d"
]
Compile: 288.905ms | Execution: 96.755ms | React with ❌ to remove this embed.
Angius
Angius3w ago
This works
mich4s
mich4sOP3w ago
so the code will be like this?
No description
Angius
Angius3w ago
yup And the separators1 line can be deleted now
mich4s
mich4sOP3w ago
yeah alright alright
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
can you explain how this throw new works? @Angius
Angius
Angius3w ago
It throws the given exception
mich4s
mich4sOP3w ago
wdym by throws
Angius
Angius3w ago
Causes an error to happen
mich4s
mich4sOP3w ago
is the throw connected to try and catch in my main segment of the code
Angius
Angius3w ago
Yes
mich4s
mich4sOP3w ago
where i have catch(ArgumentOutOfRangeException
Angius
Angius3w ago
Thrown exceptions can be caught
mich4s
mich4sOP3w ago
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 ?
Angius
Angius3w ago
Yep
MODiX
MODiX3w ago
Angius
REPL Result: Success
try {
throw new Exception("henlo");
} catch {
Console.WriteLine("Error happened!");
}
try {
throw new Exception("henlo");
} catch {
Console.WriteLine("Error happened!");
}
Console Output
Error happened!
Error happened!
Compile: 397.044ms | Execution: 24.897ms | React with ❌ to remove this embed.
mich4s
mich4sOP3w ago
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
Angius
Angius3w ago
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 it
mich4s
mich4sOP3w ago
my 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
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
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?
Angius
Angius3w ago
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 from
mich4s
mich4sOP3w ago
okay what about the img?
Angius
Angius3w ago
ArgumentOutOfRangeException override's Exception's Message property, and formats it in the way that you see It also has a constructor that takes multiple parameters
Angius
Angius3w ago
No description
Angius
Angius3w ago
Here's the string, string one you're using
mich4s
mich4sOP3w ago
i dont understand everything in it like your img
Angius
Angius3w ago
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
mich4s
mich4sOP3w ago
ohhhhh okay btw do i take the color with string into the function? or is there any other variable
Angius
Angius3w ago
Not sure what you mean Yes, you can move the coloring code and stuff into a separate function
mich4s
mich4sOP3w ago
yes but like is it static void DisplayMessage(string message, string color)
Angius
Angius3w ago
$tias
Angius
Angius3w ago
You're making this method You decide
mich4s
mich4sOP3w ago
idk how to write it sorry
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
thats what came up
Angius
Angius3w ago
What do you mean "came up"?
mich4s
mich4sOP3w ago
alt + enter to create the private static void
Angius
Angius3w ago
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)?
mich4s
mich4sOP3w ago
huh?
Angius
Angius3w ago
No description
Angius
Angius3w ago
DisplayMessage(message, color)
mich4s
mich4sOP3w ago
okay now it works kinda wait
mich4s
mich4sOP3w ago
No description
Angius
Angius3w ago
Yeah Except Console.ForegroundColor is not of type string It's of type ConsoleColor
mich4s
mich4sOP3w ago
oh okay
mich4s
mich4sOP3w ago
No description
mich4s
mich4sOP3w ago
it works now :D
Angius
Angius3w ago
Nice You could also just use it with
DisplayMessage("words to display", ConsoleColor.Red);
DisplayMessage("words to display", ConsoleColor.Red);
mich4s
mich4sOP3w ago
oh makes sense yeah thanks i guess no more questions
Want results from more Discord servers?
Add your server