Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a C# parameter with default value be null?

The code shown below shows a method Get which gets string search with default value: "".

Image showing null value for a parameter that has a default value

How can the value possibly be null when search has a non-null default value and is never changed?

like image 823
Charles Burns Avatar asked Mar 30 '13 01:03

Charles Burns


1 Answers

How can the value possibly be null when search has a non-null default value and is never changed?

If you explicitly pass null to the method (or a variable which is null), the default is not used.

The default value is only used if you call the method without the parameter in place, in which case the compiler "fills in" the default value for you. If you call the method with something, including null or a object variable which is null, you will get a null value there.

like image 93
Reed Copsey Avatar answered Sep 23 '22 13:09

Reed Copsey