If DateTime is an object and default C# parameters can only be assigned compile-time constants, how do you provide default values for objects like DateTime?
I am trying to initialize values in a POCO with a constructor, using named parameters with default values.
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Bahasa C atau dibaca “si” adalah bahasa pemrograman tingkat tinggi dan general-purpose yang digunakan dalam sehari-hari. Maksud dari general-purpose adalah bisa digunakan untuk membuat program apa saja.
Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.
DateTime cannot be used as a constant but you could make it a nullable type (DateTime?) instead.
Give the DateTime? a default value of null, and if it is set to null at the start of your function, then you can initialize it to any value you want.
static void test(DateTime? dt = null) {     if (dt == null)     {         dt = new DateTime(1981, 03, 01);     }      //... }   You can call it with a named parameter like this:
test(dt: new DateTime(2010, 03, 01));   And with the default parameter like this:
test(); 
                        The only way you can do this directly is to use the value default(DateTime), which is compile-time constant. Or you can work around that by using DateTime? and setting the default value to null.
See also this related question about TimeSpan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With