tags:

views:

1004

answers:

12
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?

+2  A: 

Plain and simple:
It creates a new Object of the Class Customer.

Henrik P. Hessel
A: 

this line instantiates your object. basically, this means you have a new instance of the Customer object that is set to its preset defaults.

Jason
+1  A: 

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

Garrett
+16  A: 

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.

daniel
+2  A: 

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.

mezoid
+1  A: 

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

chakrit
+5  A: 

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();

Jon Galloway
I don't know how most feel about this, but I would say that in a strongly typed language, this is the wrong way to go. Of course, this may just be semantics...
Richard Clayton
Sure, but as you say it is purely semantic. var is still strongly typed so there's no difference in how the code functions.
Jon Galloway
This is ony of my pet peeves with a lot of code I see online these days - I can understand using "var" if you don't know at compile time what object type you are going to get, but if you know exactly what the type is, why not just use it? It's far more readable and instantly obvious what type of object the variable represents.
TabbyCool
+1  A: 

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();
Jimmy
You're declaring a *variable* of type Customer called cust. You can declare that variable and never end up with an instance of Customer at all (it could remain null forever).
Jon Skeet
+44  A: 

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.

Jon Skeet
@Downvoter: Care to give a reason?
Jon Skeet
+3  A: 
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();

See Liskov substitution principle.

Sinan Ünür
+4  A: 

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.

Chetan Sastry
A: 

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.

Sean O'Hollaren