Menu
CoddyTech

네임스페이스와 파일

Coddy의 C++ 개발자를 위한 C# 코스 레슨. 48개 중 29번째.

C++ 네임스페이스는 ::을 사용하며, 소스 파일 연결은 헤더와 별도의 번역 단위에 의존하는 경우가 많습니다. C#의 using은 이름 검색에 영향을 주며, mcs는 제공된 .cs 파일을 하나의 어셈블리로 결합하고 C++의 include 헤더는 사용하지 않습니다. 작업을 시작하는 static Main은 정확히 하나여야 합니다.

컴파일러는 모든 소스 파일을 하나의 프로그램으로 결합합니다. 네임스페이스 이름은 형식을 구성하며, using은 이름을 범위 안으로 가져옵니다. 가져오기 스크립트처럼 파일을 실행하지는 않습니다. public은 이러한 형식 사이의 접근을 제어합니다. 클래스가 여러 개 있어도 Main은 정확히 하나만 필요합니다.

C++ 참조

#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
namespace Catalog { int total(int unit,int count) { return unit*count; } }
int main() {
cout << Catalog::total(2,5) << "\n";
}

C# 보조 선언

namespace Display { public static class Text { public static string Bracket(string value) { return "[" + value + "]"; } } }

Main 내부의 C# 문장

Console.WriteLine(Display.Text.Bracket("pen"));

이는 핵심 내용을 가르치기 위한 발췌문입니다. 과제 파일에는 실행 가능한 완전한 래퍼가 포함되어 있으며, 위 코드 조각은 관련 선언과 문장을 보여 줍니다.

참조 출력:

10

C# 출력:

[pen]
challenge icon

챌린지

쉬움

Pricing.cs에서 Catalog.Pricing.Total을 구현하세요. 이 메서드는 음이 아닌 정수 입력에 대해 unit * quantity를 반환합니다. Solve는 네임스페이스가 지정된 도우미를 호출합니다. 드라이버도 이 메서드를 직접 호출합니다.

잠금 해제된 C# 파일을 수정하세요. 화면에 표시된 잠금 파일 Main.cs는 설명된 줄을 읽고, Exercise.Solve를 호출하고, 입력이 변경되지 않았는지 확인한 다음 결과를 끝에 줄바꿈 하나를 붙여 출력합니다. 선언된 API를 유지하세요. 두 번째 Main을 추가하거나 진단 출력을 출력하지 마세요.

잠금된 실행기도 Solve 래퍼만이 아니라 내보낸 도우미 또는 클래스 API를 독립적으로 테스트합니다.

직접 해보기

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
if (Catalog.Pricing.Total(7,3) != 21 || Catalog.Pricing.Total(4,0) != 0) throw new Exception("Pricing API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

C++ 개발자를 위한 C#의 모든 레슨

직접 연습해 보세요: 온라인 C# 컴파일러