开发者

c# what's the point of type = new type()?

开发者 https://www.devze.com 2023-02-07 11:29 出处:网络
Given: myObject hello = new myObject(); What\'s the point of specifying myObject again in the second part of the line of code?

Given:

myObject hello = new myObject();

What's the point of specifying myObject again in the second part of the line of code?

Would you ever do anything like:

myObject hello = new anotherObject();

Why not just

myObject hello = 开发者_如何学JAVAnew();


Basic polymorphism. You can say myObject hello = new derivesFromMyObject().


Given the possibility of object inheritance/polymorphism then yes you may at some point do:

BaseObject A = new ChildObject();


Yes you can do

MyObject hello = new AnotherObject();

if AnotherObject is a subclass of MyObject.


Have you heard of polymorphism?

An assignment such as:

myObject hello = new();

would make it impossible, since you could not specify the actual type of the object being created.

You should not assume that the type of the variable and the type of the object that the variable points to are the same. That's the basis of polymorphism. For example:

Vehicle a = new Car();
Vehicle b = new Bike();

In most languages, the object's type must conform to the variable's type. In most OO languages, this means that the object's type must be the same type, a subtype, or an implementation (cfr. interfaces) of the variable's type.


First of all, the problem with stating the type twice has been solved, only the other way round (using the var keyword) with:

var someClass = new SomeClass();

and yes, it is often useful to have a variable declared of another type than the actual instantiation, such as:

BaseClass b = new ChildClass();

or

MyInterface i = new ClassThatImplementsInterface();


Because this is how the C# language is defined.

The specification of the type on the left side doesn't have much to do with the specification of the type on the right side. The specification of the type on the left side specified the type of the variable. The specification of the type on the right side specifies the type you want to instantiate.

The var keyword solves this issue, but the other way around from your example, so you can type:

var hello = new myObject();

Close enough I think.


I've also wondered the same question.

In case we just don't use any kind of polymorphsm and subtyping, when we create and instanciate the object on the same line (like in your example), the A a = new(); syntax should be accepted...

We can use var keyword instead if we don't want repetition, but I don't like var on known types, it makes the code a little bit more complex to read.


You have the var keyword

var hello = new myObject();

It is the same simplification, just on the other side.

The use comes when you have a class hierarchy.

class Base {}

class SubA : Base {}

class SubB : Base {}

Then you might want to have the variable declared as Base even if you instantiate SubA or SubB

The following would not work

SubA hello = new SubA();
hello = new SubB();


myObject hello = new myObject();

The "myObject hello" declares the object.

The "new myObject()" calls the constructor for myObject... It's possible that you have several constructors that take different sets of parameters to create different flavors of the object.

0

精彩评论

暂无评论...
验证码 换一张
取 消