728x90
반응형
[예제1]
키보드로 입력처리
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 키보드로 입력 처리
var input = ReadLine();
WriteLine("------------입력값--------------");
WriteLine(input.GetType());
WriteLine("------------출력값----------");
WriteLine(input + 100);
WriteLine("----------100 + 값 -------------");
WriteLine(int.Parse(input) + 100);
}
}
[예제2]
함수원형 4가지
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// 함수원형 4가지
static void f1() { }
static void f2(int a) { }
static int f3() { return 0; }
static int f4(int a) { return 0; }
static void Main(string[] args)
{
}
}
}
[추가내용]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// 함수원형 4가지
static void f1() { }
static void f2(int a) { }
static int f3() { return 0; }
static int f4(int a) { return 0; }
static void f5() { return; } // return을 만나면 함수 탈출
static void f6(int x, int y)
{
int t = x;
x = y;
y = t;
}
// swap 함수
static void f7(ref int x, ref int y)
{
int t = x;
x = y;
y = t;
}
static void Main(string[] args)
{
int a = 3, b = 4;
WriteLine("--------------원형-------------");
WriteLine(a + " " + b);
WriteLine("------------swap(x)------------");
f6(a, b);
WriteLine(a + " " + b);
WriteLine("--------------swap-------------");
f7(ref a, ref b); // // swap 함수
WriteLine(a + " " + b);
}
}
}
[예제3]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static int a = 10;
static ref int f1()
{
return ref a;
}
static void Main(string[] args)
{
WriteLine("--f1()함수는 값이 return이 된다--");
WriteLine();
int b = f1(); // (참조하고 관련이 없다)
b = 20;
WriteLine( a + " " + b);
WriteLine("--------공유가 일어 난다.--------");
WriteLine();
ref int c = ref f1();
c = 20;
WriteLine(a + " " + c);
}
}
}
[추가내용]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static int a = 10;
static ref int f1()
{
return ref a;
}
static void Main(string[] args)
{
int b = f1(); // 값이 return된다.( 참조하고 아무 상관이 없다. )
b = 20;
WriteLine(a + " " + b);
WriteLine("----------------------------");
ref int c = ref f1(); // 공유가 일어 난다.
c = 20;
WriteLine(a + " " + c);
WriteLine("----------------------------");
Tiger t = new Tiger();
int d = t.f1();
d = 30;
WriteLine(t.a + " " + d);
WriteLine("----------------------------");
ref int e = ref t.f1();
e = 40;
WriteLine(t.a + " " + e);
WriteLine();
}
class Tiger
{
public int a = 10;
public ref int f1()
{
return ref a;
}
}
}
}
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 25.2) [C#] 문법5 (Class) (0) | 2021.02.18 |
---|---|
# 25.1) [C#] 문법4 (0) | 2021.02.18 |
# 24.1) [C#] 문법2 (0) | 2021.02.17 |
# 23) [C/C++] 천 단위마다 "," 찍어서 출력하는 코드 (0) | 2021.02.16 |
# 22.2) [C#] 문법1 (0) | 2021.02.16 |