| |

VerySource

 Forgot password?
 Register
Search
View: 771|Reply: 6

What does this wording mean?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-22 11:00:01
| Show all posts |Read mode
public new string AddElementName
{
    get
    {
        return base.AddElementName;
    }
    set
    {
        base.AddElementName = value;
    }
}
"Public new string" What does the new here mean? Why add it? What is the difference between adding and not adding?
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-29 11:15:01
| Show all posts
Compile but
c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(49): The member "WebApplication1.WebForm1.AddElementName" does not hide the inherited members. The keyword new is not required.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-29 22:30:01
| Show all posts
Oh. I saw the code in a class that inherited from ConfigurationElementCollection. I don't know what that new means.
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-6-30 16:15:01
| Show all posts
Explicitly hide members inherited from the base class...

That is to hide the AddElementName inherited from the base class..
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-6-30 17:30:01
| Show all posts
When used as a modifier, the new keyword can explicitly hide members inherited from the base class. Hiding an inherited member means that the derived version of the member will replace the base class version. It is allowed to hide members without using the new modifier, but a warning will be generated. Using new to explicitly hide members cancels this warning and records the fact that a derived version is substituted.

To hide the inherited member, declare the member in the derived class with the same name, and use the new modifier to modify the member.
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-30 19:00:01
| Show all posts
new is used to change the behavior of a method or a property implemented in its base class.

Specifically in the program fragment of the landlord:
public new string AddElementName

AddElementName, is a property of the current derived class that returns a string type
This property is inherited, if you do not want this property as defined in the base class
Realize that way, you can override this property: public string AddElementName
Note that the above is not added new, so there is no problem to write, but the compiler will issue
Warning (it worried that you accidentally overwritten the AddElementName property of the base class), in order
Don't let the compiler issue a warning (also shows that you really want to override this attribute), you can
Add new, like this: public new string AddElementName
In this way, the compiler knows that you are very clear that you are overwriting the attribute and you will not be alerted.

In the code snippet of the landlord:
public new string AddElementName
{
    get
    {
        return base.AddElementName;
    }
    set
    {
        base.AddElementName = value;
    }
}

The derived class explicitly overrides the AddElementName property of the base class, but from its implementation point of view,
But it is realized by directly calling the AddElementName attribute of the base class. It feels like there is no disease and moaning.
A more appropriate analogy is: take off the quilt and fart --> do more

The above, on behalf of my personal point of view, I do not know if it is right, and then listen to the lecture downstairs.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-3 12:15:01
| Show all posts
Thank you very much for your kind answers. I see, thank you!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list