Computer/Coding Test
Exercism - Tim from Marketing , Nullable
안녕도라
2025. 1. 14. 17:05
문제요약
이름, 부서를 입력받아 특정 형식으로 출력하라
난이도
?
풀이 포인트
- Null 값 처리
https://machineboy0.tistory.com/302
C# Nullablity , Null 체크, NullReferenceException
Nullabsence of a value.C# 8.0 이전// 참조 타입: null 가능string nullableReferenceType = "hello";nullableReferenceType = null;// 값 타입 : null이 될 수 있음을 명시해줘야 했다.int nonNullableValueType = 5;nonNullableValueType = null;
machineboy0.tistory.com
REVIEW
C#에서도 Swift처럼 Optional Unwrapping 비스무리한 문법이 있었다..
C#에서 ?를 처음 본 나 아직 갈길이 멀었나보다.
그리고 아직 템플릿 문자열? 문자열 보간에 약하다.
$"{ }"를 사용하는 데 익숙해지자.
다른 사람의 모범답안을 보니 1줄로 끝날 코드를 난 함수도 만들고, 너무 복잡하게 한 듯 하다.
CODE
// 모범 코드
using System;
static class Badge
{
public static string Print(int? id, string name, string? department) => $"{(id == null ? "" : $"[{id}] - ")}{name} - {department?.ToUpper() ?? "OWNER"}";
}
// 내 코드
using System;
static class Badge
{
public static string Print(int? id, string name, string? department)
{
string answer = "";
// id 있으면 [], 없으면 생략
if(id != null){
answer += "[" + id + "] - ";
}
answer += name + " - ";
// department 있으면 대문자, 없으면 OWNER
answer += NormalizedName(department);
return answer;
}
public static string NormalizedName(string? name)
{
if(name == null)
{
return "OWNER";
}
return name.ToUpper();
}
}