DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Ukjin Yang
Ukjin Yang

Posted on

[Java vs .NET] void

2016λ…„ 8μ›” 24일 from blog.hazard.kr

Java

void λΌλŠ” νƒ€μž…μ΄ μžˆλ‹€.
μ‹¬μ§€μ–΄λŠ” Void ν΄λž˜μŠ€κΉŒμ§€ μžˆλ‹€. java.lang.Void 둜.
μ•ˆνƒ€κΉκ²Œλ„ Void 클래슀λ₯Ό 톡해 상속 λΆˆκ°€λŠ₯ν•˜λ©°, constructor 도 private 라 μ ‘κ·Ό λΆˆκ°€λŠ₯ν•˜λ‹€.
그럼 μžλ°”λŠ” μ™œ voidλ₯Ό νƒ€μž…μœΌλ‘œ λ§Œλ“€μ—ˆλƒ, λ°”λ‘œ λ¦¬ν”Œλ ‰μ…˜ λ•Œλ¬Έμ΄λ‹€.

public class Test {
 public static void main(String[] args) throws SecurityException, NoSuchMethodException {
  Class c1 = Test1.class.getMethod("Testt",null).getReturnType();
  System.out.println(c1 == Void.TYPE);
  System.out.println(c1 == Void.class);
 }
}

class Test1{
 public void Testt(){}
}
Enter fullscreen mode Exit fullscreen mode

μ—¬κΈ°μ„œ λˆˆμ—¬κ²¨λ³Ό 뢀뢄이 λ°”λ‘œ getMethod μ—μ„œ λ©”μ†Œλ“œ 정보λ₯Ό 뢈러온 λ‹€μŒ getReturnType λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ”λ°,
λ§Œμ•½ voidκ°€ ν‚€μ›Œλ“œλ‘œ 끝났을 경우 μ–˜κ°€ void인지 μ•„λ‹Œμ§€ νŒκ°€λ¦„ν•˜κΈ° μ–΄λ €μ›Œμ§„λ‹€.
이런 λ””μžμΈμœΌλ‘œ 인해 μžλ°”μ—μ„œλŠ” VoidλΌλŠ” 클래슀λ₯Ό λ§Œλ“€μ—ˆλ‹€.
그럼 voidλŠ” μ›μ‹œ νƒ€μž…μΈκ°€? λ§žλ‹€. 단지 λ‹€λ₯Έ μ›μ‹œ νƒ€μž…μ²˜λŸΌ λ³€μˆ˜ λ“±μœΌλ‘œ νƒ€μž… 선언이 μ•ˆλœλ‹€λŠ” 것 뿐이닀. (μ»΄νŒŒμΌλŸ¬κ°€ λ§‰λŠ”λ‹€)
근데 μ œλ„€λ¦­μ€ μ›ƒκΈ°κ²Œ λ¨ΉνžŒλ‹€. 즉, Future<Void> κ°€ λ¨Ήν˜€μ„œ void λžŒλ‹€μ‹μ„ μ“Έ 수 μžˆλ‹€λŠ” 것이닀. 였 μ‹œλ°œβ€¦

.NET

.NET Frameworkμ—μ„œλ„ voidλŠ” System.Void ꡬ쑰체의 λ³„μΉ­μœΌλ‘œ, μ—„λ°€νžˆ λ§ν•˜μžλ©΄ νƒ€μž…μ€ λ§žλ‹€.
ν•˜μ§€λ§Œ, μžλ°”μ™€λŠ” 달리 μ œλ„€λ¦­μœΌλ‘œλ„μ‘°μ°¨ λͺ»μ“΄λ‹€. MSDNμ—μ„œλŠ” Void ꡬ쑰체λ₯Ό μ•„λž˜μ™€ 같이 μ„€λͺ…ν•œλ‹€.

The Void structure is used in the System.Reflection namespace, but is rarely useful in a typical application. The Void structure has no members other than the ones all types inherit from the Object class.

이 κ΅¬μ‘°μ²΄λŠ” λ¦¬ν”Œλ ‰μ…˜μš© κ΅¬μ‘°μ²΄λΌλŠ” 것이닀. κ·Έ μ™Έμ—λŠ” μ“Έ 곳도 μ—†κ³ . μžλ°”μ™€ μš©λ„λŠ” λ‹€λ₯Ό 게 μ—†λ‹€.
κ·Έλ ‡λ‹€λ©΄ void λ©”μ†Œλ“œμ˜ λ¦¬ν„΄νƒ€μž…μ„ μ–΄λ–»κ²Œ κ°€μ Έμ˜€λ‚˜, μžλ°”μ™€ 같이 λ¦¬ν”Œλ ‰μ…˜μ„ μ‚¬μš©ν•΄ 보겠닀.

public class Test {
 public static void Main(string[] args) {
  Type c1 = typeof(Test1).GetMethod("Testt").ReturnType;
  Console.WriteLine(c1 == typeof(void));
  //Console.WriteLine(c1 == typeof(System.Void)); //CS0673 였λ₯˜
 }
}

class Test1{
 public void Testt(){}
}
Enter fullscreen mode Exit fullscreen mode

μžλ°”μ™€ λΉ„μŠ·ν•˜λ‹€.

근데 ν¬ν•œν•˜κ²Œ typeof(System.Void) 식이 μ•ˆλ¨ΉνžŒλ‹€. μžλ°”μ—μ„œ Void.class μ‹μœΌλ‘œ νƒ€μž… κ°€μ Έμ˜¬ 수 μžˆλ‹€λŠ” 것과 λŒ€μ‘°μ μ΄λ‹€.
κ²Œλ‹€κ°€ μžλ°”μ™€ 달리 μ œλ„€λ¦­μ„ λͺ»μ“΄λ‹€. μ΄λ ‡κ²Œ λ§‰νžŒ μ΄μœ λŠ” ECMA 335 ν‘œμ€€μ— λ”°λ₯΄κΈ° λ•Œλ¬ΈμΈλ°,
Partition II, Section 9.4 μ—μ„œ μ œλ„€λ¦­μ—μ„œ μ‚¬μš©ν•˜μ§€ λ§ν•˜μ•Ό ν•  μœ ν˜•μ„ μ•„λž˜μ™€ 같이 κΈ°μˆ ν–ˆλ‹€.

μ°Έμ‘°ν˜• ν˜•μ‹ (예: System.Generic.Collection.List 1<string&> μ΄λ ‡κ²Œ ν•˜λ©΄ μ•ˆ 됨.)
κ°’ ν˜•μ‹ 쀑 ν•„λ“œμ— CIL 평가 μ˜μ—­(μ–΄μ…ˆλΈ”λ¦¬ 상 평가식)을 κ°€λ¦¬ν‚€λŠ” μœ ν˜• (예:List<System.RuntimeArgumentHandle>)
void (예: List<System.Void> μ΄λ ‡κ²Œ ν•˜λ©΄ μ•ˆ 됨.)
즉, 이 ν‘œμ€€μœΌλ‘œ 인해 μ»΄νŒŒμΌλŸ¬μ—μ„œ Void ꡬ쑰체 μ‚¬μš©μ„ μ•„μ˜ˆ 막은 것이닀. λ¬Όλ‘  μžλ°”λ„ 닷넷에 λΉ„ν•΄ λŠμŠ¨ν•˜μ§€λ§Œ λ§‰νžŒ 건 λ§ˆμ°¬κ°€μ§€μ΄λ‹€.
λ§Œμ•½ List<System.Void> 이런 μ‹μœΌλ‘œ μ œλ„€λ¦­μ΄λ‚˜ ν• λ‹Ή μ‹μœΌλ‘œ μ‚¬μš©ν•˜λ € ν•˜λ©΄ μ»΄νŒŒμΌλŸ¬λŠ” μ•„λž˜μ™€ 같은 였λ₯˜λ₯Ό 내뱉을 것이닀.

System.Void cannot be used from C# β€” use typeof(void) to get the void type object
System.VoidλŠ” C#μ—μ„œ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. void ν˜•μ‹ 개체λ₯Ό κ°€μ Έμ˜€λ €λ©΄ typeof(void)λ₯Ό μ‚¬μš©ν•˜μ„Έμš”.
Enter fullscreen mode Exit fullscreen mode

κ·ΈλŸ¬λ―€λ‘œ void νƒ€μž…μ„ κ°€μ Έμ˜€λ €λ©΄ typeof(void) μ™Έμ—λŠ” 방법이 μ—†λ‹€.
C/C++ 처럼 void* 포인터도 κ°€μ§ˆ 수 μžˆλŠ”λ° 이 κΈ€ λ²”μ£Όμ—μ„œ λ²—μ–΄λ‚˜κ³  관련도 μ—†κΈ° λ•Œλ¬Έμ— μ„€λͺ…은 ν•˜μ§€ μ•Šκ² λ‹€.

κ·Έλ ‡λ‹€. 결둠적으둜 μ–˜κΈ°ν•˜λ©΄ μžλ°”λ‚˜ λ‹·λ„·μ΄λ‚˜ voidλŠ” λ¦¬ν”Œλ ‰μ…˜μš©μœΌλ‘œ λ©”μ†Œλ“œ λ°˜ν™˜ ν˜•μ‹μ„ μΈμ‹ν•˜κΈ° μœ„ν•΄μ„œλ§Œ μ‘΄μž¬ν•˜λŠ” νƒ€μž…μΈ 것이닀.
끗.

Top comments (0)

Find what you were looking for? Join hundreds of thousands of developers on DEV so you can:

Β 
🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content