python - How do I make the C# constructor syntax more pythonic? -


i have background in python initializer (essentially python object constructor syntax), , syntax instantiate object in python follows:

class account:      def __init__(self,name=none,address="not supplied",balance=0.0):          this.name = name          this.address=address          this.balance=balance 

why it, in c#, have provide defaults in body of constructor method, when in python can declare them optional, , default values passed in (see __init__'s signature):

public class account {     private string name;      private string address;     private decimal balance;      public account (string inname, string inaddress, decimal inbalance)     {          name = inname;          address = inaddress;          balance = inbalance;      }      public account (string inname, string inaddress)      {         name = inname;          address = inaddress;         balance = 0;     }       public account (string inname)      {          name = inname;         address = "not supplied";         balance = 0;     } } 

why can't following in c#?

public class account {     private string name;      private string address;     private decimal balance;      public account (string inname, string inaddress="not supplied", decimal inbalance=0;)     {          name = inname;          address = inaddress;          balance = inbalance;      } 

is possible have constructor syntax in c# similar (if not exact duplicate) python's initializer syntax?

this has been done using named , optional arguments (c# programming guide)

visual c# 2010 introduces named , optional arguments. named arguments enable specify argument particular parameter associating argument parameter's name rather parameter's position in parameter list. optional arguments enable omit arguments parameters. both techniques can used methods, indexers, constructors, , delegates.

the definition of method, constructor, indexer, or delegate can specify parameters required or optional. call must provide arguments required parameters, can omit arguments optional parameters.

each optional parameter has default value part of definition. if no argument sent parameter, default value used.


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -