Dailelog

2.namespace 본문

언어/CS

2.namespace

Daile 2022. 9. 2. 18:07
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 부산시
{
class 동래구
{
//visibility(가시성)
// 동래구라는 class를 다른 namespace또는 class에서 사용하려면 public을 해주어야 한다.
public static string wherAreYou()
{
return "부산시/동래구/사직동";
}
public static int add(int x, int y)
{
return x + y +100;
}
}
class Program
{
static int add(int x, int y)
{
return x + y;
}
static double add(double x, double y)
{
return x + y;
}
static string wherAreYou()
{
return "부산시/Program/Here";
}
static void Main(string[] args)
{
int ans1;
double ans2;
ans1 = add(10, 20);
Console.WriteLine(ans1);
ans1 = 동래구.add(10, 20);
Console.WriteLine(ans1);
//동일한 이름의 add함수이지만 내부코드가 다른 함수를 클래스를 정의하면서 구분이 가능하다.
ans2 = Program.add(10.1, 20.2);
Console.WriteLine(ans2);
Console.WriteLine(wherAreYou()); //이 코드가 위치하는 곳이 이미 부산시.Program에 있어서wherAreYou() 만 적어주어도 된다
Console.WriteLine(동래구.wherAreYou());
Console.WriteLine(서울시.종로구.wherAreYou()); //full name
Console.WriteLine(경기도.광주시.wherAreYou());
Console.WriteLine(전라남도.광주시.wherAreYou());
}
}
}
namespace 서울시
{
//visibility(가시성)
class 종로구
{
public static string wherAreYou()
{
return "서울시/종로구/사직동";
}
}
}
namespace 경기도
{
//visibility(가시성)
class 광주시
{
public static string wherAreYou()
{
return "경기도/광주시/Here";
}
}
}
namespace 전라남도
{
//visibility(가시성)
class 광주시
{
public static string wherAreYou()
{
return "전라남도/광주시/Here";
}
}
}

 [namespace 의 역확]
- 식별자(identifier)가 영향을 미치는 범위 지정
-대규모의 과제 수행시 식별자가 중복되는 현상을 고나리
- 주소와 비슷한 용도
  부산시 동래구 사직동
  서울시 종로구 사직동
  경기도 광주
 전라남도 광주

namespace는 한글도 사용가능하다.

namespace안에 동일한 이름을 가졌지만 타입이 다른함수를 인식가능하다.
하지만 한 class안에 동인한 이름과 타입 동인한 아규먼트까지 가지고 있으면
컴파일러는 인식하지 못한다.

하지만 똑같은 static 함수를 다른 namespace 또는 class에는 사용할수 있다.

지금 대학에서는  namespace를 각별하게 사용할 일은 없다고 한다. 하지만 다양한 사람과 함께 프로젝트나

업무를 할때 매우 중요하기 때문의 개념을 잘 인지 해야 한다. 

반응형
LIST

'언어 > CS' 카테고리의 다른 글

4.Recursive Function 재귀함수  (3) 2022.09.07
3. C# 기본형  (0) 2022.09.05
1.C-> C# (sum)  (0) 2022.09.02