c# - How to specify equivalant of null as default parameter of struct type -



c# - How to specify equivalant of null as default parameter of struct type -

how can define function struct parameter can optionally passed, yet able know within function whether passed or not? trying accomplish similar to:

function somefunction(string str = null) { if (str == null) { ... } }

calling above function no parameter trigger if (str == null)... condition:

somefunction();

the above approach doesn't work parameter of type struct. given next struct:

public struct mystruct { public int someint; public double somedouble; }

this generates error there no standard conversions:

public function somefunction(mystruct mystruct = null) { }

but when define function as:

public function somefunction(mystruct mystruct = default(mystruct)) { }

and phone call function with:

somefunction();

on entry function mystruct contains 0 someint , 0.00 somedouble. these legit values leaves me without way know if parameter passed function or left empty.

how can define parameter if not specified in phone call can observe within function?

use nullable types (optionally overloaded methods shown below dosomething) check whether value passed or not.

public function somefunction(mystruct? mystruct) { if(mystruct.hasvalue) dosomething(mystruct.value); else dosomething(); }

c# struct

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -