2023. 3. 16. 21:09ㆍChlln's Code/C#
기본 데이터 형식
Ch03/IntegralType/MainApp.cs
using System;
namespace IntegralTypes
{
class MainApp
{
static void Main(string[] args)
{
sbyte a = -10;
byte b = 40;
Console.WriteLine($"a = {a}, b = {b}");
short c = -30000;
ushort d = 60000;
Console.WriteLine($"c = {c}, d = {d}");
int e = -1000_0000; // 0이 7개
uint f = 3_0000_0000; // 0이 8개
Console.WriteLine($"e = {e}, f = {f}");
long g = -5000_0000_0000; // 0이 11개
ulong h = 200_0000_0000_0000_0000; // 0이 18개
Console.WriteLine($"g = {g}, h = {h}");
}
}
}
출력 결과
a = -10, b = 40
c = -30000, d = 60000
e = -10000000, f = 300000000
g = -500000000000, h = 2000000000000000000
Ch03/IntagerLiterals/MainApps.cs
using System;
namespace IntegerLiterals
{
class MainApp
{
static void Main(string[] args)
{
byte a = 240; // 10진수 리터럴
Console.WriteLine($"a = {a}");
byte b = 0b1111_0000; // 2진수 리터럴
Console.WriteLine($"b = {b}");
byte c = 0XF0; // 16진수 리터럴
Console.WriteLine($"c = {c}");
uint d = 0x1234_abcd; // 16진수 리터럴
Console.WriteLine($"d = {d}");
}
}
}
출력 결과
a = 240
b = 240
c = 240
d = 305441741
Ch03/SignedUnsigned/MainApp.cs
using System;
namespace SignedUnsigned
{
class MainApp
{
static void Main(string[] args)
{
byte a = 255; // byte 형식 255는 1111 1111
sbyte b = (sbyte)a; // (sbyte)는 변수를 sbyte 형식으로 변환하는 연산자
Console.WriteLine(a
+ "\n" + b);
}
}
}
출력 결과
255
-1
Ch03/Overflow/MainApp.cs
using System;
namespace Overflow
{
class MainApp
{
static void Main(string[] args)
{
uint a = uint.MaxValue; // uint의 최대값, 4294967295
Console.WriteLine(a);
a = a + 1;
Console.WriteLine(a);
}
}
}
출력 결과
4294967295
0
부동 소수점 형식
Ch03/FloatingPoint/MainApp.cs
using System;
namespace FloatingPoint
{
class MainApp
{
static void Main(string[] args)
{
float a = 3.1415_9265_3589_7932_3846f; // float 형식 변수에 값을 직접 할당하려면 숫자 뒤에 f를
// 붙여줘야 함.
Console.WriteLine(a);
double b = 3.1415_9265_3589_7932_3846;
Console.WriteLine(b);
}
}
}
출력 결과
3.1415927
3.141592653589793
Ch03/Decimal/MainApp.cs
using System;
namespace Decimal
{
class MainApp
{
static void Main(string[] args)
{
float a = 3.1415_9265_3589_7932_3846_2643_3832_79f; // 숫자 뒤에 f를 붙이면 float 형식으로 간주
double b = 3.1415_9265_3589_7932_3846_2643_3832_79; // 아무것도 없으면 double
decimal c = 3.1415_9265_3589_7932_3846_2643_3832_79m; // m을 붙이면 decimal
Console.WriteLine(a + "\n"
+ b + "\n"
+ c);
}
}
}
출력 결과
3.1415927
3.141592653589793
3.1415926535897932384626433833
문자 형식과 문자열 형식
Ch03/Char/MainApp.cs
using System;
namespace Char
{
class MainApp
{
static void Main(string[] args)
{
char a = '안',
b = '녕',
c = '하',
d = '세',
e = '요';
Console.Write(a + ""
+ b + ""
+ c + ""
+ d + ""
+ e + "\n");
}
}
}
출력 결과
안녕하세요
Ch03/String/MainApp.cs
using System;
namespace String
{
class MainApp
{
static void Main(string[] args)
{
string a = "안녕하세요?",
b = "칠런입니다.";
Console.WriteLine(a + "\n"
+ b);
}
}
}
출력 결과
안녕하세요?
칠런입니다.
논리 형식
Ch03/Bool/MainApp.cs
using System;
namespace Bool
{
class MainApp
{
static void Main(string[] args)
{
bool a=true,
b=false;
Console.WriteLine(a + "\n"
+ b);
}
}
}
출력 결과
True
False
object 형식
Ch03/Object/MainApp.cs
using System;
namespace Object
{
class MainApp
{
static void Main(string[] args)
{
object a = 123,
b = 3.141592653589793238462643383279m,
c = true,
d = "안녕하세요.";
Console.WriteLine(a + "\n"
+ b + "\n"
+ c + "\n"
+ d);
}
}
}
출력 결과
123
3.1415926535897932384626433833
True
안녕하세요.
박싱과 언박싱
Ch03/BoxingUnboxing/Program.cs
using System;
namespace BoxingUnboxing
{
class Program
{
static void Main(string[] args)
{
int a = 123;
object b = (object)a; // a에 담긴 값을 박싱해서 힙에 저장
int c = (int)b; // b에 담긴 값을 언박싱해서 스택에 저장
Console.WriteLine(a + "\n"
+ b + "\n"
+ c);
double x = 3.14159265;
object y = x; // x에 담긴 값을 박싱해서 힙에 저장
double z = (double)y; // y에 담긴 값을 언박싱해서 스택에 저장
Console.WriteLine(x + "\n"
+ y + "\n"
+ z);
}
}
}
출력 결과
123
123
123
3.14159265
3.14159265
3.14159265
데이터 형식 바꾸기
Ch03/IntegralConversion/Program.cs
using System;
namespace IntegralConversion
{
class Program
{
static void Main(string[] args)
{
sbyte a = 127;
Console.WriteLine(a);
int b = (int)a;
Console.WriteLine(b);
int x = 128; // sbyte의 최대값 127보다 1 큰 수
Console.WriteLine(x);
sbyte y = (sbyte)x; // 오버플로우 발생
Console.WriteLine(y);
}
}
}
출력 결과
127
127
128
-128
Ch03/FloatConversion/Program.cs
using System;
namespace FloatConversion
{
class Program
{
static void Main(string[] args)
{
float a = 69.6875f;
Console.WriteLine($"a : {a}");
double b = (double)a;
Console.WriteLine($"b : {b}");
Console.WriteLine($"69.6875 == b : {69.6875 == b}");
float x = 0.1f;
Console.WriteLine($"x : {x}");
double y= (double)x;
Console.WriteLine($"y : {y}");
Console.WriteLine($"0.1 == y : {0.1 == y}");
}
}
}
출력 결과
a : 69.6875
b : 69.6875
69.6875 == b : True
x : 0.1
y : 0.10000000149011612
0.1 == y : False
Ch03/SignedUnsignedConversion/Program.cs
using System;
namespace SignedUnsignedConversion
{
class Program
{
static void Main(string[] args)
{
int a = 500;
Console.WriteLine(a);
uint b = (uint)a;
Console.WriteLine(b);
int x = -30;
Console.WriteLine(x);
uint y = (uint)x; // 언더플로우
Console.WriteLine(y);
}
}
}
출력 결과
500
500
-30
4294967266
Ch03/FloatToIntegral/Program.cs
using System;
namespace FloatToIntegral
{
class Program
{
static void Main(string[] args)
{
float a = 0.9f;
int b = (int)a;
Console.WriteLine(b);
float c = 1.1f;
int d = (int)c;
Console.WriteLine(d);
}
}
}
출력 결과
0
1
Ch03/StringNumberConversion/Program.cs
using System;
namespace StringNumberConversion
{
class Program
{
static void Main(string[] args)
{
int a = 123;
string b = a.ToString();
Console.WriteLine(b);
float c = 3.14f;
string d = c.ToString();
Console.WriteLine(d);
string e = "123456";
int f = Convert.ToInt32(e);
Console.WriteLine(f);
string g = "1.2345";
float h = float.Parse(g);
Console.WriteLine(h);
}
}
}
출력 결과
123
3.14
123456
1.2345
상수와 열거 형식
Ch03/Constant/Program.cs
using System;
namespace Constant
{
class Program
{
static void Main(string[] args)
{
const int MAX_INT = 2147483647,
MIN_INT = -2147483647;
Console.WriteLine(MAX_INT + "\n"
+ MIN_INT);
}
}
}
출력 결과
2147483647
-2147483647
열거 형식 - 여러 개의 상수를 정리
Ch03/Enum/Program.cs
using System;
namespace Enum
{
class Program
{
enum DialogResult { YES, NO, CANCEL, CONFIRM, OK }
static void Main(string[] args)
{
Console.WriteLine((int)DialogResult.YES + "\n"
+ (int)DialogResult.NO + "\n"
+ (int)DialogResult.CANCEL + "\n"
+ (int)DialogResult.CONFIRM + "\n"
+ (int)DialogResult.OK);
}
}
}
출력 결과
0
1
2
3
4
Ch03/Enum2/Program.cs
using System;
namespace Enum2
{
class Program
{
enum DialogResult { YES, NO, CANCEL, CONFIRM, OK }
static void Main(string[] args)
{
DialogResult result = DialogResult.YES;
Console.WriteLine((result == DialogResult.YES) + "\n"
+ (result == DialogResult.NO) + "\n"
+ (result == DialogResult.CANCEL) + "\n"
+ (result == DialogResult.CONFIRM) + "\n"
+ (result == DialogResult.OK));
}
}
}
출력 결과
True
False
False
False
False
Ch03/Enum3/Program.cs
using System;
namespace Enum3
{
class Program
{
enum DialogResult { YES = 10, NO, CANCEL, CONFIRM = 50, OK }
static void Main(string[] args)
{
Console.WriteLine((int)DialogResult.YES + "\n"
+ (int)DialogResult.NO + "\n"
+ (int)DialogResult.CANCEL + "\n"
+ (int)DialogResult.CONFIRM + "\n"
+ (int)DialogResult.OK);
}
}
}
출력 결과
10
11
12
50
51
Nullable 형식
Ch03/Nullable/Program.cs
using System;
namespace Nullable
{
class Program
{
static void Main(string[] args)
{
int? a = null;
Console.WriteLine(a.HasValue);
Console.WriteLine(a != null);
a = 3;
Console.WriteLine(a.HasValue);
Console.WriteLine(a != null);
Console.WriteLine(a.Value);
}
}
}
출력 결과
False
False
True
True
3
var : 데이터 형식을 알아서 파악하는 똑똑한 C# 컴파일러
Ch03/UsingVar/Program.cs
using System;
namespace UsingVar
{
class Program
{
static void Main(string[] args)
{
var a = 20;
Console.WriteLine($"Type : {a.GetType()}, Value : {a}");
var b = 3.141592;
Console.WriteLine($"Type : {b.GetType()}, Value : {b}");
var c = "Hello, World!";
Console.WriteLine($"Type : {c.GetType()}, Value : {c}");
var d = new int[] { 10, 20, 30 };
Console.Write($"Type : {d.GetType()}, Value : ");
foreach (var e in d)
Console.Write($"{e} ");
Console.WriteLine();
}
}
}
출력 결과
Type : System.Int32, Value : 20
Type : System.Double, Value : 3.141592
Type : System.String, Value : Hello, World!
Type : System.Int32[], Value : 10 20 30
공용 형식 시스템
Ch03/CTS/Program.cs
using System;
namespace CTS
{
class Program
{
static void Main(string[] args)
{
System.Int32 a = 123;
int b = 456;
Console.WriteLine($"a type : {a.GetType().ToString()}, value : {a}");
Console.WriteLine($"b type : {b.GetType().ToString()}, value : {b}");
System.String c = "abc";
string d = "def";
Console.WriteLine($"c type : {c.GetType().ToString()}, value : {c}");
Console.WriteLine($"d type : {d.GetType().ToString()}, value : {d}");
}
}
}
출력 결과
a type : System.Int32, value : 123
b type : System.Int32, value : 456
c type : System.String, value : abc
d type : System.String, value : def
문자열 안에서 찾기
Ch03/StringSearch/Program.cs
using static System.Console;
namespace StringSearch
{
class Program
{
static void Main(string[] args)
{
string greeting = "Good Morning";
WriteLine(greeting + "\n");
// IndexOf()
WriteLine($"IndexOf 'Good' : {greeting.IndexOf("Good")}");
WriteLine($"IndexOf 'o' : {greeting.IndexOf("o")}");
// LastIndexOf()
WriteLine($"LastIndexOf 'Good' : {greeting.LastIndexOf("Good")}");
WriteLine($"LastIndexOf 'o' : {greeting.LastIndexOf("o")}");
// StartWith()
WriteLine($"StartWith 'Good' : {greeting.StartsWith("Good")}");
WriteLine($"StartWith 'Morning' : {greeting.StartsWith("Morning")}");
// EndsWith()
WriteLine($"EndsWith 'Good' : {greeting.EndsWith("Good")}");
WriteLine($"EndsWith 'Morning' : {greeting.EndsWith("Morning")}");
// Contains()
WriteLine($"Contains 'Evening' : {greeting.Contains("Evening")}");
WriteLine($"Contains 'Morning' : {greeting.Contains("Morning")}");
// Replace()
WriteLine($"Replace 'Morning' : {greeting.Replace("Morning", "Evening")}");
}
}
}
출력 결과
Good Morning
IndexOf 'Good' : 0
IndexOf 'o' : 1
LastIndexOf 'Good' : 0
LastIndexOf 'o' : 6
StartWith 'Good' : True
StartWith 'Morning' : False
EndsWith 'Good' : False
EndsWith 'Morning' : True
Contains 'Evening' : False
Contains 'Morning' : True
Replace 'Morning' : Good Evening
문자열 변형하기
Ch03/StringModify/Program.cs
using static System.Console;
namespace StringModify
{
class Program
{
static void Main(string[] args)
{
WriteLine($"ToLower() : '{"ABC".ToLower()}'");
WriteLine($"ToUpper() : '{"abc".ToUpper()}'");
WriteLine($"Insert() : '{"Happy Friday!".Insert(5, " Sunny")}'");
WriteLine($"Remove() : '{"I Don't Love You".Remove(2, 6)}'");
WriteLine($"Trim() : '{" No Spaces ".Trim()}'");
WriteLine($"TrimStart() : '{" No Spaces ".TrimStart()}'");
WriteLine($"TrimEnd() : '{" No Spaces ".TrimEnd()}'");
}
}
}
출력 결과
ToLower() : 'abc'
ToUpper() : 'ABC'
Insert() : 'Happy Sunny Friday!'
Remove() : 'I Love You'
Trim() : 'No Spaces'
TrimStart() : 'No Spaces '
TrimEnd() : ' No Spaces'
문자열 분할하기
Ch03/StringSlice/Program.cs
using System;
using static System.Console;
namespace StringSlice
{
class Program
{
static void Main(string[] args)
{
string greeting = "Good morning.";
WriteLine(greeting.Substring(0, 5)); // "Good"
WriteLine(greeting.Substring(5) + "\n"); // "morning."
string[] arr = greeting.Split(new string[] { " " }, StringSplitOptions.None);
WriteLine($"Word Count : {arr.Length}");
foreach (string element in arr)
WriteLine($"{element}");
}
}
}
출력 결과
Good
morning.
Word Count : 2
Good
morning.
문자열 서식 맞추기
Ch03/StringFormatBasic/Program.cs
using System;
using static System.Console;
namespace StringFormatBasic
{
class Program
{
static void Main(string[] args)
{
string fmt = "{0,-20}{1,-15}{2,30}";
WriteLine(fmt, "Publisher", "Auther", "Title");
WriteLine(fmt, "Marvel", "Stan Lee", "Iron Man");
WriteLine(fmt, "Hanbit", "Sanghyun Park", "This is C#");
WriteLine(fmt, "Prentice Hall", "K&R", "The C Programming language");
}
}
}
출력 결과
Publisher Auther Title
Marvel Stan Lee Iron Man
Hanbit Sanghyun Park This is C#
Prentice Hall K&R The C Programming language
Ch03/StringFormatNumber/Program.cs
using System;
using static System.Console;
namespace StringFormatNumber
{
class MainApp
{
static void Main(string[] args)
{
// D : 10진수
WriteLine($"10진수 : {123:D}");
WriteLine($"10진수 : {123:D5}");
// X : 16진수
WriteLine($"16진수 : 0x{0xFF1234:X}");
WriteLine($"16진수 : 0x{0xFF1234:X8}");
// N : 숫자
WriteLine($"숫자 : {123456789:N}");
WriteLine($"숫자 : {123456789:N0}");
// F : 고정소수점
WriteLine($"고정소수점 : {123.45:F}");
WriteLine($"고정소수점 : {123.456:F5}");
// E : 공학용
WriteLine($"공학 : {123.456789:E}");
}
}
}
출력 결과
10진수 : 123
10진수 : 00123
16진수 : 0xFF1234
16진수 : 0x00FF1234
숫자 : 123,456,789.00
숫자 : 123,456,789
고정소수점 : 123.45
고정소수점 : 123.45600
공학 : 1.234568E+002
Ch03/StringFormatDatetime/Program.cs
using System;
using System.Globalization;
using static System.Console;
namespace StringFormatDatetime
{
class Program
{
static void Main(string[] args)
{
DateTime dt = new DateTime(2023, 3, 16, 17, 40, 50);
WriteLine($"12시간 형식 : {dt:yyyy-MM-dd tt hh:mm:ss (ddd)}");
WriteLine($"24시간 형식 : {dt:yyyy-MM-dd HH:mm:ss (dddd)}");
CultureInfo ciKo = new CultureInfo("ko-KR");
WriteLine();
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciKo));
WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciKo));
WriteLine(ciKo);
CultureInfo ciEn = new CultureInfo("en-US");
WriteLine();
WriteLine(dt.ToString("yyyy-MM-dd tt hh:mm:ss (ddd)", ciEn));
WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss (dddd)", ciEn));
WriteLine(ciEn);
}
}
}
출력 결과
12시간 형식 : 2023-03-16 오후 05:40:50 (목)
24시간 형식 : 2023-03-16 17:40:50 (목요일)
2023-03-16 오후 05:40:50 (목)
2023-03-16 17:40:50 (목요일)
ko-KR
2023-03-16 PM 05:40:50 (Thu)
2023-03-16 17:40:50 (Thursday)
en-US
Ch03/StringInterpolation/Program.cs
using System;
using static System.Console;
namespace StringInterpolation
{
class Program
{
static void Main(string[] args)
{
string name = "김칠런";
int age = 26;
WriteLine($"{name,-10}, {age:D3}");
name = "김트루피";
age = 26;
WriteLine($"{name}, {age,-10:D3}");
name = "손플라누스";
age = 26;
WriteLine($"{name}, {(age > 20 ? "성인" : "미성년자")}");
}
}
}
출력 결과
김칠런 , 026
김트루피, 026
손플라누스, 성인
'Chlln's Code > C#' 카테고리의 다른 글
[C#] 03 : 연습 문제 (0) | 2023.03.16 |
---|---|
[C#] 02 : 연습 문제 (0) | 2023.03.16 |
[C#] 02 : 처음 만드는 C# 프로그램 (0) | 2023.03.16 |