본문 바로가기
Programming/C#

[C#] static, readonly 등 modifier 순서 강제하기 (.editorconfig)

by 째스터 2024. 8. 14.
728x90

이전에 다루었던 C# Coding Style 중에 이런 규칙이 있다.
( 2024.07.05 - [Programming/C#] - C# Coding Style )

When used on static fields, readonly should come after static (e.g. static readonly not readonly static).

.editorconfig 파일은 다음과 같이 순서가 설정되어 있다.

csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async

readonly static, static readonly 모두 사용해도 별도의 warning이 없었다.
coding rule에 따른다면 static readonly가 맞는데 순서가 바뀌어도 warning이 따로 발생하지 않고 있었다.

.editorconfig 파일에 csharp_preferred_modifier_order에서 지정한 설정 뒤에 ": suggestion"을 붙여주면 된다.
그러고 다시 소스코드로 돌아가보면 잘못된 순서인 readonly static의 경우 IDE0036 에러가 발생하게 된다.
(이제 Fix all occurrences in Solution을 클릭해서 전체 솔루션에서 modifier 순서 수정이 가능하다.)

 

은근히 헷갈리기도 하고 유지가 되고 있지 않아서 회사 등에서 협업할 때 필수로 설정해야겠다.


참고자료: https://stackoverflow.com/a/33237262/19392359

 

Is there a convention to the order of modifiers in C#?

If I were to use more than one, what order should I use modifier keywords such as: public, private, protected, virtual, abstract, override, new, static, internal, sealed, and any others I'm forget...

stackoverflow.com

 

728x90

댓글