C
C#2d 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
mich4sOP2d ago
No description
mich4s
mich4sOP2d 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
MODiX2d 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
Angius2d ago
0b prefix 0o for octal, 0x for hexadecimal
mich4s
mich4sOP2d ago
since when was octal added? also how to convert from binary to hexadecimal and store it in one variable like int hexNumber
Jimmacle
Jimmacle2d ago
and .ToString("B") will turn the number into a binary string in the most recent versions of .NET
Angius
Angius2d ago
Integer is integer Hex, oct, dec, bin No matter It's the same value
mich4s
mich4sOP2d ago
yeah i know but like uhhh i want it to display by default in the given number system
Angius
Angius2d ago
That :X displays in hex :B would display in bin, IIRC
Jimmacle
Jimmacle2d ago
the default is decimal, any other format you need the format specifier
Jimmacle
Jimmacle2d 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
MODiX2d 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
mich4sOP2d 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
MODiX2d 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
Angius2d ago
(start_index, length)
mich4s
mich4sOP2d ago
thats all?
Angius
Angius2d ago
ye
mich4s
mich4sOP2d ago
oh that seems easy
Angius
Angius2d ago
Also
mich4s
mich4sOP2d ago
yeah
leowest
leowest2d ago
bonus points if its ranges
MODiX
MODiX2d 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
Angius2d ago
This takes all letters from index 1 (inclusive) to index 5 (exclusive)
mich4s
mich4sOP2d ago
i only had the .Substring thing on my lessons
Angius
Angius2d 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
mich4sOP2d 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
Angius2d ago
Yeah, makes sense
mich4s
mich4sOP2d ago
its my 3rd month of learning from the scratch never touched c# before
mich4s
mich4sOP2d ago
No description
mich4s
mich4sOP2d 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
Angius2d ago
Like, directly there, without a variable?
MODiX
MODiX2d 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
Angius2d ago
This works
mich4s
mich4sOP2d ago
so the code will be like this?
No description
Angius
Angius2d ago
yup And the separators1 line can be deleted now
mich4s
mich4sOP2d ago
yeah alright alright
mich4s
mich4sOP2d ago
No description
mich4s
mich4sOP2d ago
can you explain how this throw new works? @Angius
Angius
Angius2d ago
It throws the given exception
mich4s
mich4sOP2d ago
wdym by throws
Angius
Angius2d ago
Causes an error to happen
mich4s
mich4sOP2d ago
is the throw connected to try and catch in my main segment of the code
Angius
Angius2d ago
Yes
mich4s
mich4sOP2d ago
where i have catch(ArgumentOutOfRangeException
Angius
Angius2d ago
Thrown exceptions can be caught
mich4s
mich4sOP2d 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
Angius2d ago
Yep
MODiX
MODiX2d 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
mich4sOP2d 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
Angius2d 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
mich4sOP2d 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
mich4sOP2d ago
No description
mich4s
mich4sOP2d 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
Angius2d 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
mich4sOP2d ago
okay what about the img?
Angius
Angius2d 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
Angius2d ago
No description
Angius
Angius2d ago
Here's the string, string one you're using
mich4s
mich4sOP2d ago
i dont understand everything in it like your img
Angius
Angius2d 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
mich4sOP2d ago
ohhhhh okay btw do i take the color with string into the function? or is there any other variable
Angius
Angius2d ago
Not sure what you mean Yes, you can move the coloring code and stuff into a separate function
mich4s
mich4sOP2d ago
yes but like is it static void DisplayMessage(string message, string color)
Angius
Angius2d ago
$tias
Angius
Angius2d ago
You're making this method You decide
mich4s
mich4sOP2d ago
idk how to write it sorry
mich4s
mich4sOP2d ago
No description
mich4s
mich4sOP2d ago
thats what came up
Angius
Angius2d ago
What do you mean "came up"?
mich4s
mich4sOP2d ago
alt + enter to create the private static void
Angius
Angius2d 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
mich4sOP2d ago
huh?
Angius
Angius2d ago
No description
Angius
Angius2d ago
DisplayMessage(message, color)
mich4s
mich4sOP2d ago
okay now it works kinda wait
mich4s
mich4sOP2d ago
No description
Angius
Angius2d ago
Yeah Except Console.ForegroundColor is not of type string It's of type ConsoleColor
mich4s
mich4sOP2d ago
oh okay
mich4s
mich4sOP2d ago
No description
mich4s
mich4sOP2d ago
it works now :D
Angius
Angius2d ago
Nice You could also just use it with
DisplayMessage("words to display", ConsoleColor.Red);
DisplayMessage("words to display", ConsoleColor.Red);
mich4s
mich4sOP2d ago
oh makes sense yeah thanks i guess no more questions
Want results from more Discord servers?
Add your server