Customer cust = new Customer();
Customer
is a class. cust
is an assigned name. I'm not sure what Customer()
does...
What does this line do? Why do we need it? Isn't having Customer
and Customer()
a bit repetitive?
Customer cust = new Customer();
Customer
is a class. cust
is an assigned name. I'm not sure what Customer()
does...
What does this line do? Why do we need it? Isn't having Customer
and Customer()
a bit repetitive?
Plain and simple:
It creates a new Object of the Class Customer.
this line instantiates your object. basically, this means you have a new instance of the Customer object that is set to its preset defaults.
This is not something only C# does, Objective C and Java do this as well. You need to define the class you will be using for that variable. Then what you are confused about, that part is what initiates the class and assigns it to the variable.
NSString *string = [[NSString alloc] initWithString:@"Test"];
You might want to look into Typcasting
It declares a Customer and then initializes it.
Customer cust; //declares a new vaariable of Customer type
cust = new Customer(); //Initializes that variable to a new Customer().
new creates the actual object, cust hold's a reference to it.
The empty parentheses indicates that the construction of the Customer object is being passed no parameters, otherwise there would be a comma separated list of parameters within the parenthesis.
It is declaring a Customer
object called cust
and assigning it a new instance of the class with no parameters being passed to the object's constructor.
If you hate repetitiveness like I do, use var
var cust = new Customer();
... and cust is now strongly-and-statically-typed as an instance of Customer
Customer() is the constructor method on the Customer class. If you're bothered by the repetition, you can use a implicit variable declaration:
var cust = new Customer();
use
var cust = new Customer();
to avoid the repetition. In general the pattern is there so that you
1) declare an instance of Customer called cust 2) initialize it. not that you could have other ways of initializing like
Customer cust = CustomerProvider.NextCustomer();
It creates a new instance of Customer()
and assigns a reference to the newly created object to the variable cust
.
If you want to remove the repetition and you're using C# 3.0 or later and it's a local variable, you can use:
var cust = new Customer();
That has exactly the same meaning - it's still statically typed, i.e. the variable cust
is still very definitely of type Customer
.
Now, it happened to be repetitive in this case, but the two Customer
bits are entirely separate. The first is the type of the variable, the second is used to say which constructor to call. They could have been different types:
Customer cust = new ValuedCustomer();
IClient cust = new Customer();
object cust = new Customer();
etc. It's only because you created an instance of exactly the same type as the type of the variable that the repetition occurred.
Customer cust
explicitly declares cust
to be of type Customer
.
Customer cust = new Customer();
initializes it by constructing a new Customer
.
See also Implicitly Typed Local Variables (C# Programming Guide).
Remember that the LHS variable does not have to be of identical type to the object that is being constructed on the RHS. For example, if Customer
is a subclass of Contact
,
Contact cust = new Customer();
The first Customer defines the datatype of the cust variable. The new Customer()
part creates an instance of the Customer class and assigns it to the variable.
It is not required however that the datatype be Customer
. If you have the Customer class inherit a different class (say Person) or an interface (say IPayer), you could define it as
Person cust = new Customer();
IPayer cust = new Customer();
This is one of the basic principles of Polymorphism in object-oriented programming.
This is C#'s java-esque style showing. This is the exact syntax you would use in Java to declare a new object.
In case you're still confused, although there are already great answers, it may make it less confusing if you think of "Customer cust" just as you would "int i". You're declaring a new variable (cust) of a certain type (Customer).
If you're new to object oriented programming, it just takes some patience. OO thinking is a bit confusing at first, but once it clicks, you'll get it for life.