using System;
class aaaaaaaaaaaaaaaaaa
{
static void Main()
{
double x, y;
byte b;
inti;
char;
x = 10.0;
y = 3.0;
i=(int)(x/y); // cast from double to int type
Console.WriteLine("Integer outcome of x/y:"+i);
i = 100;
b=(byte)i;
Console.WriteLine("Value of b:"+b);
i = 257;
b=(byte)i;
Console.WriteLine("Value of b:"+b);
b = 88;
ch=(char)b;
Console.WriteLine("ch:"+ch);
}
}
What I don't understand is the last line.
If I build it here, it will be ch:x, but why is it x?
Also, I learned that char is the role of keeping only one character, but is it meaningful?
c#
The reason is that if you substitute an integer and cast it as a char type, it becomes "X".
As you know, char is a type that expresses only one character (*1), so when outputting to the console, the input "88" (*2) was interpreted as Unicode and the capital letter "X" corresponding to Unicode "88" was printed.
How to evaluate the value "88" (*2) depends on the type.So, on the contrary,
inti=(int)'X';
Console.WriteLine(i);
When you write like this, "88" will be printed.
(*1) Actually, it's not that easy around here.Sometimes two char types represent one character.
(*2) For convenience, we use the decimal number "88", but please understand that it is essentially the value stored in memory.
There is a string type that is good at handling characters, but I think you are wondering why char type exists again.
Char types can be handled more efficiently, and a typical use point is
I sometimes use it around.
It's a little complicated, but there's a big difference between the reference type for the string type and the value type for the char type.
In the case of the reference type, the substance is placed in the management heap, so you will need to ask the garbage collector to do more work.On the other hand, since the char type is a value type, garbage collectors don't have to do extra work because they don't use the management heap if they use it in general.
Sometimes the processing granularity is relatively large and you don't have to worry about the overhead around here, but in some scenarios, for example, the cost of processing and the overhead of using the reference type may be the same or the worst case scenario.
So, I personally think that's why there are so many overloads of type string and type char around here.
Similarly, if you want to separate and process strings one by one, it is more efficient to process them as char types than as single-character strings (*3)
(*3) However, you may have two chars and one character as shown above, so you need to be careful about that.
US>As a prerequisite, the information on the computer is basically a combination of two states (bits) of 0/1, where char
is a 16-bit number between 0 and 65535 with characters assigned to each integer (UTF-16).For these reasons, character types are essential for string expressions on your computer.
The char
type overrides ToString
and returns string
with the characters corresponding to UTF-16 above.So if you use the + operator of the string combination, char
is not a number but a character.
© 2024 OneMinuteCode. All rights reserved.