In C #, which means "Customer cust = new Customer ();" do?

Customer cust = new Customer(); 

Customer is a class. cust is the assigned name. I'm not sure what Customer() does ...

What does this line do? Why do we need this? Does Customer and Customer() multiple duplicates?

+6
c #
source share
12 answers

It declares the client and then initializes it.

 Customer cust; //declares a new variable of Customer type cust = new Customer(); //Initializes that variable to a new Customer(). 

new creates the actual object, holds back the reference to it.

Empty parentheses indicate that the construction of the Customer object is not passed in by any parameters, otherwise a list of parameters separated by commas will be indicated in brackets.

+22
source share

Creates a new instance of Customer() and assigns a reference to the newly created object to the cust variable.

If you want to remove the repetition, and you are using C # 3.0 or later, and this is a local variable, you can use:

 var cust = new Customer(); 

This has exactly the same meaning - it is still statically typed, i.e. The cust variable is still very definitely of type Customer .

Now in this case it repeated, but two bits of Customer completely separated. The first is the type of variable, the second is to indicate which constructor should be called. They could be of different types:

 Customer cust = new ValuedCustomer(); IClient cust = new Customer(); object cust = new Customer(); 

etc .. This only happens because you created an instance of exactly the same type as the type of the variable in which the repetition occurred.

+49
source share

Client () is the constructor method of the Customer class. If you are worried about repetition, you can use the declaration of an implicit variable:

var cust = new Customer ();

+5
source share

The first client defines the data type of the cust variable. The new Customer() instantiates the Customer class and assigns it to a variable.

However, the data type is not required to be Customer . If you have a class Customer that inherits another class (say Person) or an interface (say IPayer), you can define it as

 Person cust = new Customer(); IPayer cust = new Customer(); 

This is one of the basic principles of Polymorphism in object-oriented programming.

+4
source share
 Customer cust 

explicitly declares cust type Customer .

 Customer cust = new Customer(); 

Initializes it by building a new Customer .

See also Implicitly Typed Local Variables (C # Programming Guide) .

Remember that the LHS variable does not have to be of the same type for an object that is built on RHS. For example, if Customer is a subclass of Contact ,

 Contact cust = new Customer(); 

See Liskov Substitution Principle .

+3
source share

Simple and simple:
It creates a new class Client object.

+2
source share

It declares a Customer object with the name cust and assigns it a new instance of the class without any parameters passed to the constructor of the object.

+2
source share

This is not what only C #, Objective-C, and Java do. You need to define the class that you will use for this variable. Then, what are you confused about, this part initiates the class and assigns it to a variable.

 NSString *string = [[NSString alloc] initWithString:@"Test"]; 

You might want to learn Typcasting.

+1
source share

If you hate repeating like me, use var

 var cust = new Customer(); 

... and cust is now strongly and statically typed as an instance of Customer

+1
source share

use

 var cust = new Customer(); 

to avoid repetition. In general, the pattern exists, so you

1) declare a client instance named cust 2) initialize it. not that you might have other initialization methods like

 Customer cust = CustomerProvider.NextCustomer(); 
+1
source share

this line creates an instance of your object. this basically means that you have a new instance of the Customer object that is configured with its predefined default values.

0
source share

This shows the C # Java style style. This is the exact syntax that you would use in Java to declare a new object.

If you are still confused, although there are already excellent answers, this may make it less confusing if you think of “customer containment” as well as “int i”. You declare a new variable (cust) of a specific type (Customer).

If you are new to object-oriented programming, this requires some patience. OO thinking is a bit confusing at first, but as soon as it clicks, you get it for life.

0
source share

All Articles