I program in C# and Python, so here's a view from someone who recently went from C# to Python:
C# provides a default constructor if you do not create one.
So, by default (if you didn't have a constructor already), you could instantiate the object with its defaults like follows:
var Account = new Account();
This would instantiate those properties with their respective default values.
If you provide a constructor, you must then provide the default constructor as well (if you intend on constructing an object just by: var account = new Account();
:
public Account() {} //default constructor
public Account(string name)
{
Name = name;
}
The other primitive types in your class are initialized according to their default values.
If you'd like something Pythonic, you need to be sure you're using C# 4.0; we call them Optional Parameters. Their usage (in your case) would be exactly as you wrote it:
public string Name { get; set;}
public string Address {get; set;}
public decimal Balance {get; set;}
public Account (string name, string address="not supplied", decimal balance=0;)
{
Name = name;
Address = address;
Balance = balance;
}
A few style comments:
In C#, we use properties. They aren't used quite the same way in Python, and in Python you'd be more likely to use fields. Our properties are PascalCased.
We don't use 'in' and 'out' parameters as part of the parameter name (there's no need to, we have ref
and out
for when we need to pass references -- sadly something that is missing from Python (although not really sad because it makes code more complex)).
In C#, fields are Camel cased -- or camelCased
, as it were. In Python, they'd follow Unix conventions: first_name
as opposed to firstName.
In C#, your properties are public
by default, and they should be. In Python, everyone assumes public
, private
is only assumed if an underscore precedes the variable -- not so in C#.