Just in Chronicles

Life as a Voyage

Can a Constructor in a Class Return a Value?

The answer is NO.

In .NET, constructors in a class can

  • Not return any value,
  • Throw exceptions, and
  • Be overloaded.

On the other hand, constructors in a class developed by PHP can return a value including NULL.

Personally, I support .NET’s stance because constructors should be used only for initialising a class instance. I agree that the PHP way gives much convenience to developers.

Therefore, if developers want to return a value after initialising a class, simply implement a method such ToString(), ToInt32(), or ToObject().

PHP와 달리 닷넷에서는 생성자가 에러를 제외한 반환값을 만들 수 없다. 만약 어떤 클라스 인스턴스를 만든 후에 뭔가를 반환하고 싶다면 ToString() 또는 ToObject() 같은 메써드를 통해 하는 것이 바람직하다.


Written by Justin Yoo

03/11/2009 at 22:39

Posted in For .NET

Tagged with ,