Language - C#/C#(문법)

C# - Coding, Naming Convention

KimTory 2021. 11. 8. 13:10

Microsoft Docs 참조

https://docs.microsoft.com/en-us/visualstudio/ide/reference/options-text-editor-csharp-formatting?view=vs-2019 

 

C# editor formatting options - Visual Studio (Windows)

Learn how to use the Formatting options page and its subpages to set options for formatting code in the code editor when you are programming in C#.

docs.microsoft.com

 

▣ Visual Studio, Layout 설정

 

→ 레이아웃 규칙

1. 기본 코드 편집기 설정을 사용(4글자 들여 쓰기,  Tab을 공백으로 저장)  →  Ctrl + K + D

2. 선언을 한 줄에 하나씩만 작성
3. 메서드 정의와 속성 정의 간에는 빈 줄을 하나 이상 추가

 

 

▣ C#, Naming 표준

 

1. 파스칼 표기 : 모든 단어의 첫 문자는 대문자이며, 나머지는 소문자로 표기

-. Class, Function

public class GetObject
{
    public void GetPixel()
    {
    }
}


2. 카멜 표기 : 처음 사용된 단어 제외하고, 첫 번째 문자만 대문자로 표시하고 나머진 소문자로 표기

-. 변수

int totalPixel;

void GetToPixel(int pixel)
{
    int pixels = pixel;
}

 

▣ C#, 주석 표기법

 

1. 코드 줄의 끝이 아닌, 코드 상단부에 주석을 배치하여 사용하는 게 일반적

2. 대문자로 주석 텍스트를 시작(한글 X, 영어만 해당)

3. 주석 구분 기호 즉, // 과 주석 문자 사이 공백을 Default로 삽입

// 올바른 주석 위치
if (totalPixel == 0) // 잘못된 주석 삽입 위치
{
}

// GetTotal Pixel Object
if (totalPixel == 0)
{
}

 

▣ C#, 언어 규칙 (Delegate, Interface, 예외 처리 문법)

 

1. Delegate / 파스칼 표기법 

using System;

namespace FileLoad
{
    public delegate void GetPixel(double dpixel);
    class Program
    {
        public static void GetToPixel(double dpixel)
        {
            Console.WriteLine($"Get Pixel :  {dpixel}"); // 문자열 보간법
        }
        static void Main(string[] args)
        {
            GetPixel tempDel1 = GetToPixel;
            GetPixel tempDel2 = new GetPixel(GetToPixel);
        }
    }
}

2. Interface / 'I' 가 포함된 파스칼 표기법

using System;
using System.Globalization;

namespace FileLoad
{
    // Interface 선언 시, 'I' 포함된 파스칼 표기 사용
    interface ILogger
    {
        void WriteLog(string message);
    }
    class ClimateMonitor
    {
        private ILogger logger;
        public ClimateMonitor(ILogger logger) => this.logger = logger;
    }
    class ConsoleLogger : ILogger
    {
        public void WriteLog (string message)
        {
            DateTime dt = DateTime.Now.ToLocalTime();
            CultureInfo ci = new CultureInfo("ko-KR");
            string dtFormat = dt.ToString("yyyy-MM-dd HH:mm:ss (ddd)", ci);
            Console.WriteLine($"{dtFormat} + {message}");
        }
    }

3. 예외 처리 문법

bool bToWhile = true;
using (Mat image = new Mat(new OpenCvSharp.Size(400, 400), MatType.CV_8UC1))
{
    if(bToWhile) image.SetTo(new Scalar(255));
    else image.SetTo(new Scalar(0));

    Cv2.ImShow("Image", image);
}
// using 구문 종료 시, 자동으로 Dispose

Mat image1 = new Mat(new OpenCvSharp.Size(400, 400), MatType.CV_8UC1);
try
{ 
    if(bToWhile) image1.SetTo(new Scalar(255));
    else image1.SetTo(new Scalar(0));

    Cv2.ImShow("Image", image1);
}
catch
{
}
finally
{
    image1.Dispose();
}

 

'Language - C# > C#(문법)' 카테고리의 다른 글

C# - Visual Studio Python 연동 - 2  (0) 2021.11.10
C# - Visual Studio Python 연동 - 1  (0) 2021.11.10
C# - Effective C# 개정판, 매개 변수  (0) 2021.11.08
C# - Enum.Parse, Enum.TryParse  (0) 2021.11.08
C# - DirectoryInfo Class  (0) 2021.11.05