728x90
μ μ λ°μ΄ν° νμ
λ°μ΄ν° νμ | μ€λͺ | ν¬κΈ°(byte) | λ΄μ μ μλ κ°μ λ²μ |
byte | λΆνΈ μλ μ μ | 1(8bit) | 0 ~ 255 (2^8) |
sbyte | μ μ | 1(8bit) | -128 ~ 127 (2^8) |
short | μ μ | 2(16bit) | -32,768 ~ 32,767 (2^16) |
ushort | λΆνΈ μλ μ μ | 2(16bit) | 0 ~ 65,535 (2^16) |
int | μ μ | 4(32bit) | -2^31 ~ 2^31-1 |
uint | λΆνΈ μλ μ μ | 4(32bit) | 0 ~ 2^32-1 |
long | μ μ | 8(64bit) | -2^63 ~ 2^63-1 |
ulog | λΆνΈ μλ μ μ | 8(64bit) | 0 ~ 2^64-1 |
char | μ λμ½λ λ¬Έμ | 2(16bit) |
- λΆνΈκ° μλ νμκ³Ό μλ νμ μ¬μ΄μμ λ³νμ μλͺ»νλ©΄ μ€λ²νλ‘μ°/μΈλνλ‘μ°κ° λ°μν μ μλ€.
(μμμ½λ)
namespace SignedUnsignedConversion
{
class MainApp
{
static void Main(string[] args)
{
int a = -30;
Console.WriteLine("a is " + a);
uint b = (uint)a;
Console.WriteLine("b is " + b);
}
}
}
Console>>
a is -30
b is 4294967266
μ€μ λ°μ΄ν° νμ
λ°μ΄ν° νμ | μ€λͺ | ν¬κΈ°(byte) | λ΄μ μ μλ κ°μ λ²μ |
float | λ¨μΌ μ λ°λ λΆλ μμμ νμ (7μ리κΉμ§) |
4(32bit) | -3.402823e38 ~ 3.402823e38 |
double | 볡μ μ λ°λ λΆλ μμμ νμ (15~16μ리κΉμ§) |
8(64bit) | -1.79769313486232e308 ~ 1.79769313486232e308 |
decimal | 29μ리 λ°μ΄ν°λ₯Ό ννν μ μλ μμ νμ | 16(128bit) |
- λΆλμμμ μ μ λ°λμ λ¬Έμ λλ¬Έμ, μμμ κ³μ°μλ λ³νμ μ€ λ νμ μ‘°μ¬ν΄μΌνλ€.
(μμ μ½λ)
using System;
namespace FloatConversion
{
class MainApp
{
static void Main(string[] args)
{
float a = 12.3456f;
Console.WriteLine("a is {0}", a);
double b = (double)a;
Console.WriteLine("b is {0}", b);
Console.WriteLine("λ κ°κ° κ°λμ? {0}" + (a == b));
Console.WriteLine("12.3456μ΄λ aκ° κ°λμ? " + (12.3456 == a));
Console.WriteLine("12.3456μ΄λ bκ° κ°λμ? " + (12.3456 == b));
float a1 = 0.1f;
Console.WriteLine("a1 is {0}", a1);
double b1 = (double)a1;
Console.WriteLine("b1 is {0}", b1);
Console.WriteLine("λ κ°κ° κ°λμ? " + (a1 == b1));
Console.WriteLine("0.1μ΄λ a1κ° κ°λμ? " + (0.1 == a1));
Console.WriteLine("0.1μ΄λ b1κ° κ°λμ? " + (0.1 == b1));
}
}
}
Consol >>
a is 12.3456
b is 12.345600128173828
λ κ°κ° κ°λμ? {0}True
12.3456μ΄λ aκ° κ°λμ? False
12.3456μ΄λ bκ° κ°λμ? False
a1 is 0.1
b1 is 0.10000000149011612
λ κ°κ° κ°λμ? True
0.1μ΄λ a1κ° κ°λμ? False
0.1μ΄λ b1κ° κ°λμ? False
- μ€μμ μ μ μ¬μ΄μ λ³νμμλ μμμ μ΄νλ₯Ό λ²λ¦°λ€.
- μ«μλ₯Ό λ¬Έμμ΄λ‘ λ³νν λλ μ«μ.ToString() λ©μλλ₯Ό μ¬μ©νλ€.
- λ¬Έμμ΄μ μ«μλ‘ λ³νν λλ λ κ°μ§ λ°©λ²μ΄ μλ€.
1. int.Parse(): nullκ°μ΄ λ€μ΄κ°λ©΄ ArgumentNullException
2. Convert.ToInt32(): nullκ°μ΄ λ€μ΄κ°λ©΄ 0
(μμ μ½λ)
string str = "123";
string str2 = null;
Console.WriteLine("using int.Parse: " + int.Parse(str));
Console.WriteLine("using Convert.ToInt32: " + Convert.ToInt32(str));
// null
try
{
int result1 = int.Parse(str2);
Console.WriteLine("using int.Parse: " + result1);
}
catch (Exception e)
{
Console.WriteLine("Exception using int.Parse: " + e.Message);
}
try
{
int result2 = Convert.ToInt32(str2);
Console.WriteLine("using Convert.ToInt32: " + result2);
}
catch (Exception e)
{
Console.WriteLine("Exception using Convert.ToInt32: " + e.Message);
}
Console >>
using int.Parse: 123
using Convert.ToInt32: 123
Exception using int.Parse: Value cannot be null. (Parameter 's')
using Convert.ToInt32: 0
- μ΄κ±° νμ: μ’ λ₯λ κ°μ§λ§ λ€λ₯Έ κ°μ κ°μ§ μμλ€μ μ μΈν λ μ¬μ©νλ©΄ νΈλ¦¬νλ€.
class MainApp
{
enum Status { YES = 10, NO = 20, DELETE = 30}
static void Main(string[] args)
{
Console.WriteLine(Status.YES);
Console.WriteLine((int)Status.YES);
Console.WriteLine(Status.NO);
Console.WriteLine((int)Status.NO);
Console.WriteLine(Status.DELETE);
Console.WriteLine((int)Status.DELETE);
}
}
Console >>
YES
10
NO
20
DELETE
30
- nullable νμμΌλ‘ λ§λλ €λ©΄ νμ λ€μ ?λ₯Ό λΆμ΄λ©΄ λλ€.
int nullTest = null; //int κ°μλ nullμ΄ λ€μ΄κ° μ μμ
int? nullTest1 = null; //nullableλ‘ λ§λ€ μ μμ
Console.WriteLine(nullTest1.HasValue); //nullμ΄λ―λ‘ False μΆλ ₯
- varμ μμμ ν΄λΉ νμ μ μ€μ ν΄μ€λ€.
var a = 3; //aλ intνμ
μΌλ‘ μλ μ€μ
var b = "test"; //bλ stringνμ
μΌλ‘ μλ μ€μ
728x90
'π₯οΈ > C#' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C#] λ¬Έμμ΄ μμ λ§μΆκΈ° (Format(), λ¬Έμμ΄ λ³΄κ°) (0) | 2023.08.15 |
---|---|
[C#] λ¬Έμμ΄(string) λ€λ£¨κΈ° (0) | 2023.08.15 |
[μ΄κ²μ΄ C#μ΄λ€] Chapter2 μ°μ΅λ¬Έμ (0) | 2023.08.14 |
[C#] μμ λ‘ λ°°μ°λ C# 07: ν΄λμ€ μ μΈ, μμ±μ, μ κ·Όμ νμ (0) | 2023.07.30 |
[C#] c# κ³μ°κΈ° λ§λ€κΈ° (0) | 2023.07.29 |