30.01.2010, 00:05 | #1 |
Участник
|
mfp: Optional parameters in C# and X++
Источник: http://blogs.msdn.com/mfp/archive/20...n-c-and-x.aspx
============== In the upcoming 4.0 version of C# the language now supports optional parameters – just like X++. The following is a valid method declaration in both languages: X++: public void foo(int parameter1, int parameter2 = 0, int parameter3 = 100) { } Named parameters In X++ you can only omit optional parameters starting from the last parameter. C# makes it possible to omit any optional parameter. For example: foo(5, parameter3: 1000) will call foo(5, 0, 1000). prmIsDefault C# doesn’t provide a way to determine, if a parameter was omitted by the caller, or the caller passed in a value identical to the default value. In X++ the prmIsDefault() method can be used to determine this. However, the use cases for this method are rare – and often fragile. For example, consider this X++ class: X++: class BaseClass { int value; public void init(int _value = 0) { if (prmIsDefault(value)) { value = /* some complex calculation */ } else { value = _value; } } } X++: { str myStr; public void init(int _value = 0) { myStr = 'foo'; super(_value); } } To solve the problem MyClass needs to be implemented as: X++: class MyClass extends BaseClass { str myStr; public void init(int _value = 0) { myStr = 'foo'; if (prmIsDefault(_value)) super(); else super(_value); } } X++: class BaseClass { int value; public void init() { int _value = /* some complex calculation */ new(_value); } public void init(int _value) { value = _value; } } ============== Источник: http://blogs.msdn.com/mfp/archive/20...n-c-and-x.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|