C#
용도
- 유니티 게임/AR/VR/MR
- WinForm (C++,C#) (한컴오피스)
- WebApp - Node, Java, Python
- 아두이노,라즈베리 - C/C++
기초
namespace example
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
형변환
using System;
using System.Numerics;
namespace Examples
{
internal class ex02
{
static void Main()
{
int myInt = 10;
long myLong = myInt;
long myLong2 = 1234_5678_9000;
Console.WriteLine(myLong2);
int myInt2 = (int)myLong2;
Console.WriteLine(myInt2);
int myInt3 = 10;
float myFloat = myInt3;
float myFloat2 = 3.14f;
int myInt4 = (int)myFloat2;
float a = 100;
int a_change = (int)a;
Console.WriteLine("연습문제1 : " + a_change);
double py = 3.14;
int py_change = (int)py;
Console.WriteLine("연습문제2 : " + py_change);
int b = 200;
long b_change = (long)b;
Console.WriteLine("연습문제3 : " + b_change);
long c = 20000000000;
int c_change = (int)c;
Console.WriteLine("연습문제4 : " + c_change);
}
}
}
콘솔 입력
using System;
using System.Collections.Generic;
namespace Examples
{
internal class ex03
{
static void Main()
{
Console.Write("숫자를 입력하세요 : ");
string input_num = Console.ReadLine();
int num = int.Parse(input_num);
Console.WriteLine(input_num + " X 10 = " + num * 10);
Console.Write("1 ~ 100 사이의 숫자를 입력하세요 : ");
string input_num2 = Console.ReadLine();
int num2 = int.Parse(input_num2);
if (num2 % 2 == 0)
{
Console.WriteLine(num2 + " = 짝수");
}
else
{
Console.WriteLine(num2 + " = 홀수");
}
Console.Write("문자1를 입력해주세요 : ");
string word1 = Console.ReadLine();
Console.Write("문자2를 입력해주세요 : ");
string word2 = Console.ReadLine();
Console.Write("문자1 + 문자2 = " + word1 + word2);
}
}
}
조건문
using System;
namespace Examples {
internal class ex05 {
static void Main() {
if (true) {
Console.WriteLine("조건식이 true임");
}
int num = 30;
if (num == 10) {
Console.WriteLine("10입니다.");
} else if (num == 20) {
Console.WriteLine("20입니다.");
} else if (num == 30) {
Console.WriteLine("30입니다.");
} else {
Console.WriteLine("그 외의 수 입니다.");
}
if (true) {
if (true) {
} else {
}
}
}
}
}
연산자
- 연산자
- 산술연산자 : + - * / %
- 증감연산자 : ++ --
- 관계연산자 : == != < > <= >=
- 논리연산자 : &&(and), ||(or), !(not)
- 삼항연산자 : ? :
- 대입연산자 : = 복합(+= -= *= /=)
- null 조건부 연산자 : ?.. ?[]
- null 병함 연산자 : ??
연습문제
using System;
namespace Examples {
internal class ex_0930 {
static void Main() {
Console.WriteLine("\n<문제1>");
int num = 123;
Console.WriteLine("백의 자릿수 : " + num/100);
Console.WriteLine("십의 자릿수 : " + (num%100)/10);
Console.WriteLine("일의 자릿수 : " + num%10);
Console.WriteLine("\n<문제2>");
Console.Write("숫자1를 입력하세요 : ");
string input_num1 = Console.ReadLine();
int num1 = int.Parse(input_num1);
Console.Write("숫자2를 입력하세요 : ");
string input_num2 = Console.ReadLine();
int num2 = int.Parse(input_num2);
if(num1 > num2) {
Console.WriteLine(num1);
} else {
Console.WriteLine(num2);
}
Console.WriteLine("\n<문제3>");
Console.Write("문자를 입력하세요 : ");
string str = Console.ReadLine();
if (str != null) {
Console.WriteLine(str.Length);
}
Console.WriteLine("\n<문제4>");
string input = null;
if(input == null) {
Console.WriteLine("null값입니다.");
}
}
}
}