Related articles:
[You must know. NET] The third time: Historical entanglements: attributes and attributes
[You must know. NET] The fourth time: from behind: Class and struct
This article describes the following:
Object-oriented Basic concepts
New keyword in simple words
Memory management for object creation
1. Introduction
The garden seems to have no or rarely put the new keyword out to say, then I take a chance, hehe. So is it really necessary for us to make a long speech about a keyword? It seems to be a problem. The key to the answer is: Do you really understand new? If so, please do not waste time, if not, please continue this article's sequential journey.
The following several questions may examine you to the new master, before opening, hoped everybody does a test, if passed, directly turns off this page can. If not, I hope this article will help you find out the answer.
What's the difference between a new class object and a new struct or enum?
New in. NET has several uses, what else can you do in addition to creating an object instance?
New operator, can I overload it?
What does new play in the paradigm?
What's the difference between a new inherited method and a override method?
What is the difference between int i and int i = new int ()?
2. Basic Concepts
Generally speaking, the new keyword is in. NET is used in the following situations, which is a typical explanation for MSDN:
As an operator for creating objects and calling constructors.
The focus of this article is focused on in the next section.
is used as a modifier to hide an inherited member from a base class member.
As a modifier, the basic rule can be summed up as: To implement a hidden method in a derived class, the base class method must be defined as a virtual;new as a modifier, and cannot coexist with override when the base class member is hidden, because the two semantics repel each other: The new is used to create a new member. Hides the same members with the same name as the base class, and override is used to implement extensions to the base class members.
In addition, if the data members of the base class are hidden in the subclass, access to the base class's original data member can be done by using the base modifier.
For example:
New as modifier
using System;
Namespace Anytao.net.My_Must_net
{
class number
{
public static int i = 123;
br> public void Showinfo ()
{
Console.WriteLine ("Base class---");
Public virtual void Shownumber ()
{
Console.WriteLine (i.tostring ());
}
class Intnumber:number
{
New public static int i = 456;
br> public new virtual void Showinfo ()
{
Console.WriteLine ("Derived Class---");
public override void Shownumber ()
{
Console.WriteLine ("Base nu Mber is {0} ", number.i.tostring ());
Console.WriteLine ("New number is {0}", i.ToString ());
}
}
class Tester
{
public static void Main (string[] args)
{
Number num = new number ();
Num. Shownumber ();
Intnumber intnum = new Intnumber ();
Intnum.shownumber ();
Number number = new Intnumber ();
Who actually called?
Number. Showinfo ();
//Who did you call?
Number. Shownumber ();
}
}
}