728x90
반응형
[예제1]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] ar = new int[5];
int[] br = new int[] {10, 20, 30};
WriteLine(ar.Length);
WriteLine(br.Length);
}
}
}

[예제2]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] ar = new int[5];
int[] br = new int[] {10, 20, 30};
WriteLine(ar.Length);
WriteLine(br.Length);
// 간략화된 for 문
foreach (int data in br) // data. value, item
{
WriteLine(data);
}
}
}
}

[예제3]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
// 배열의 길이를 구한다.
WriteLine(args.Length);
// 2장
// (단축키 : wl) : using static System.Console; 선언된 이후
WriteLine("Hello {0}", 100);
}
}
}

[예제4]
// a를 int 타입으로 만든다. (= boxing)
// object타입은 대입을 할때 캐스팅을 해서 사용한다.
object a = 20;
// unboxing
int b = (int)a;
int c = 20;
object d = c;
short e;
int f = 10;
e = (short)f; // 아주 자주 발생하는 프로그램
f = (int)e;
// 부호가 없는 정수타입 (= 음수가 없는 정수 타입)
// ubyte, ushort, uint, ulong 등
// 용도: 절대 음수값을 가지지 않는 경우
// Color색상번호, Soundvolume, 나이, ...
int errorNum1; // 성공: 양수 실패(음수)
uint errorNum2; // 성공: 양수 실패 0
[예제5]
const int 예제 및 enum(열거형) >> #define을 묶어 놓은 것이다.
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
enum DialogResult // void Main() 위에 작성
{
YES,
NO,
CANCEL,
CONFIRM,
OK
}
static void Main(string[] args)
{
DialogResult dr = DialogResult.OK;
}
}
}
[예제6]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int? a = null;
WriteLine(a.HasValue);
try
{
WriteLine(a.Value);
}
catch(Exception e)
{
//WriteLine("{0}", e);
}
a = 100;
WriteLine(a.HasValue);
WriteLine(a.Value);
WriteLine(a != null);
}
}
}

[예제7]
int a = 10; // c#의 형식
System.Int32 b = 20; // 클래스 형식
[예제8]
문자열 다루기 : "Apple 무궁화 Banana" >> CRUD
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
WriteLine("---------------string-------------");
// 문자열 다루기 : "Apple Banana" >> CRUD
string s1 = "Apple 무궁화 Banana";
WriteLine(s1);
WriteLine("---------------IndexOf-------------");
// 인덱스는 0번부터 시작
WriteLine(s1.IndexOf("Bana")); // 6이 리턴
// 못찾았다면?
WriteLine(s1.IndexOf("Cana")); // -1이 리턴
// 중복Data가 있다면?
WriteLine(s1.IndexOf("Bana")); // 6이 리턴
WriteLine("---------------IndexOf-------------");
// 출력 형식
WriteLine(s1.IndexOf("Bana")); //6 이 리턴
WriteLine("IndexOf : {0}", s1.IndexOf("Bana")); // 실무에서 사용하는 형식
WriteLine("IndexOf : " + " " + s1.IndexOf("Bana")); // 실무에서 사용하는 형식
WriteLine("--------------LastIndexOf--------------");
// 끝(뒤)에서부터 찾는다.
WriteLine(s1.LastIndexOf("Bana"));
// 지정된 문자열로 시작혹은 끝나는지를 평가하는 함수
WriteLine("--------------StartsWith, EndsWith--------------");
WriteLine(s1.StartsWith("Apple"));
WriteLine(s1.StartsWith("Bana"));
WriteLine(s1.EndsWith("Apple"));
WriteLine(s1.EndsWith("Bana"));
WriteLine("-------------Contains---------------");
//문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환
WriteLine(s1.Contains("Apple"));
WriteLine("-------------Replace---------------");
// Apple을 Oreange로 바꿔주는 함수
//정석: 원본data는 변경하지 않는다. 복사본을 만들어 주는것이 좋음
string s2 = s1.Replace("Apple", "Orange");
WriteLine(s1);
WriteLine(s2);
//
string s3 = s1.Replace("Kiwi", "Orange");
WriteLine(s3);
//
string s4 = s1.Replace("Banana", "Tiger");
WriteLine(s4);
// 한글에 대한 논리
string s5 = s1.Replace("무궁화", "소나무");
WriteLine(s5);
WriteLine("-------------ToLower()---------------");
//소문자로 변환된 이 문자열의 복사본을 반환
string s6 = "Tiger";
WriteLine(s6.ToLower());
WriteLine(s6);
WriteLine("-------------ToLower(), ToUpper()---------------");
//원본 변경 안된다.
string s7 = "Tiger";
WriteLine("Tiger".ToLower());
WriteLine("Tiger".ToUpper()); //대문자
WriteLine("Tiger Lion".Insert(6, "cat "));
WriteLine("--------------remove--------------");
// (X) index 2번부터 index 4번까지(포함) 삭제
// (X) index 2번부터 index 4번까지(미포함) 삭제
// (O) index 2번부터 4개를 삭제
WriteLine("Tiger Lion".Remove(2, 4)); // ger_삭제(TiLion 출력)
WriteLine("---------------trim-------------");
// Trim
WriteLine("Tiger Lion".Trim());
// 앞뒤 공백 제거(중간공백은 해당 안된다)
WriteLine(" Tiger Lion ".Trim());
WriteLine("---------------trim start,end-------------");
// start, end trim
WriteLine(" Tiger Lion ".TrimStart());
Write(" Tiger Lion ".TrimEnd());
WriteLine("tttt");
}
}
}

[예제9]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
WriteLine("--------------Substring-------------");
string s1 = "Good Morning.";
// 1인수: 시작위치 2인수: 개수
WriteLine(s1.Substring(0, 6));
WriteLine("--------------Substring-------------");
// 시작위치 부터 마지막까지
WriteLine(s1.Substring(3));
WriteLine("--------------Split-------------");
string[] br = "apple,banana,reange,kiwi".Split
(
new string[] {" "},
StringSplitOptions.None
);
foreach (var item in br)
{
WriteLine(item);
}
}
}
}

[예제10]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
//string s1 = string.Format( "{0}banana", "apple" );
WriteLine(string.Format("{0}banana", "apple"));
WriteLine("--------------Substring-------------");
// -는 좌측 +는 우측
WriteLine(string.Format("{0, -10}banana", "apple"));
WriteLine(string.Format("{0, 10}banana", "apple")); //+ 기호 사용하면 안됨
}
}
}

[예제11]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
//string s1 = string.Format( "{0}banana", "apple" );
WriteLine(string.Format("{0}banana", "apple"));
WriteLine("--------------Format-------------");
// -는 좌측 +는 우측
WriteLine(string.Format("{0, -10}banana", "apple"));
WriteLine(string.Format("{0, 10}banana", "apple")); //+ 기호 사용하면 안됨
WriteLine("--------------fmt-------------");
//
string fmt = "{0,-20}{1,-15}{2,30}";
WriteLine("{0}{1}", 100, 200);
WriteLine(fmt, "tiger", "호랑이", "캘리포니아");
WriteLine(fmt, "lion", "사자", "뉴질랜드");
WriteLine(fmt, "eagle", "독수리", "샌디에이고");
WriteLine("-------------------------------");
//
WriteLine("{0:D}", 255);
WriteLine("{0:D}", 0xff);
WriteLine("{0:D}", 255);
WriteLine("{0:D}", 0xff);
WriteLine("{0:D}", 12345678);
WriteLine("-------------------------------");
// 소수점 이하 자리를 0자리로 만든다.
string s1 = string.Format("{0:N}", 12345678);
WriteLine(s1);
}
}
}

[예제12]
현재시간(전체 년 도 월 일 시간) 알아보기
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
string s = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
WriteLine(s);
WriteLine("----------------------------------------");
//년
string year = DateTime.Now.ToString("yyyy");
WriteLine(year + "년");
WriteLine("----------------------------------------");
//월
string month = DateTime.Now.ToString("MM");
WriteLine(month + "월");
WriteLine("----------------------------------------");
//일
string day = DateTime.Now.ToString("dd");
WriteLine(day + "일");
WriteLine("----------------------------------------");
//초
string second = DateTime.Now.ToString("ss");
WriteLine(second + "초");
WriteLine("----------------------------------------");
//요일명(영문)
WriteLine(DateTime.Now.DayOfWeek + "요일");
//요일명(한글1)
WriteLine("----------------------------------------");
string[] ss = new string[] { "월", "화", "수", "목", "금","토" };
int dw = (int)DateTime.Now.DayOfWeek;
WriteLine(ss[dw] + "요일");
//요일명(한글2)
WriteLine("----------------------------------------");
APPLE app = (APPLE)dw;
WriteLine(app + "요일");
//요일명(한글3)
WriteLine("----------------------------------------");
WriteLine((APPLE)DateTime.Now.DayOfWeek + "요일");
}
enum APPLE { 월, 화, 수, 목, 금, 토 }
}
}

[예제13]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// arg가 인수 전달을 받는 예제가 있는데 무시한다.
static void Main(string[] args)
{
string name = "홍길동";
int age = 10;
WriteLine("----------------------------------------");
// 문자열 format (옛날것)
string r = string.Format("{0} {1}", name, age);
WriteLine(r);
WriteLine("----------------------------------------");
// 문자열 보간 (최신작품)
WriteLine($"{name} {age}");
WriteLine("----------------------------------------");
WriteLine($"{name, -10} {age:D4}");
WriteLine("----------------------------------------");
// {age, -10:D4} {첨자(변수), 좌우맞춤:형식지정}
WriteLine($"{name} {age, -10:D4}");
WriteLine("----------------------------------------");
string rs = $"{name} " + $"{(age>17?"성인":"미성년")}";
WriteLine(rs);
WriteLine("----------------------------------------");
name = "이순신";
age = 20;
string rs2 = $"{name} {(age > 17 ? "성인" : "미성년")}";
WriteLine(rs2);
}
}
}

728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 24.1) [C#] 문법2 (0) | 2021.02.17 |
---|---|
# 23) [C/C++] 천 단위마다 "," 찍어서 출력하는 코드 (0) | 2021.02.16 |
# 22.1) [C#] 시작하기2 (0) | 2021.02.16 |
# 21) [C#] 시작하기1 (0) | 2021.02.15 |
# 20) [Oracle] install 및 setting 방법 (0) | 2021.02.03 |