C # keywords

Source: Internet
Author: User

Keywords used to modify classes, methods, attributes, and fields:

First, the simplest explanation is private, protected, internal, and public.
Public and internal modifier classes.
Public, protected, and private modifier.
When modifying a class:
Public indicates public, that is, all the Assembly can access this class.
Internal indicates the internal class, that is, the class can be accessed only in the same assembly. Generally, the same assembly is the same dll.
When modifying the method:
Public indicates public, that is, all classes can access this method.
Protected indicates that the method is protected, that is, only my subclass can access this method.
Private indicates private, that is, only I can access this method.
Next we will discuss the differences between const and readonly:
First, both are constants. The difference is that const is the compile-time volume, and readonly is the run-time volume.
Compile time-to-time: during compilation, the value is already a constant and will be replaced with a constant value wherever the variable is used.
Running time: the value cannot be modified during running. Copy codeThe Code is as follows: public class Test
{
Public const string constStr = "this is a test ";
Public readonly string readonlyStr = "this can't be modified in runtime time ";
Public void Method1 ()
{
String s1 = constStr; // during compilation, this sentence will be replaced with string s1 = "this is a test ";
ReadonlyStr = "error ";
// Try to modify readonlyStr. Compilation fails,
// Throw an error: the readonly field can be modified only during the constructor and initialization.
}
}


OK. Now we will discuss the following keywords: abstract, virtual, new, override:
Abstract indicates abstraction. abstract can modify classes and methods.
When modifying a class:
This is called an abstract class, which has the following properties:
Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors. accessors are actually methods.
You cannot use the sealed modifier to modify the abstract class, because the two modifiers have the opposite meanings. Classes using the sealed modifier cannot be inherited, while abstract modifiers require class inheritance.
A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
When modifying the Method:
This is called an abstract method. Its nature is as follows:
An abstract method is an implicit virtual method (virtual methods are called virtual methods ).
Only abstract method declaration can be used in abstract classes. As long as abstract methods are used, this is an abstract class.
The abstract method declaration does not provide actual implementation, so there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). For example:
Public abstract void MyMethod ();
The implementation is provided by an override method override, which is a non-abstract class member.
It is incorrect to use the static or virtual modifier in the abstract method declaration. Because the abstract method needs to be overwritten, the static modifier cannot be used, because the abstract method is an implicit virtual method, therefore, virtual modifiers cannot be used.
Apart from the differences in the declaration and call syntax, abstract attributes act the same way as abstract methods. attributes are essentially methods.
It is incorrect to use the abstract modifier for static attributes.
In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Virtual keywords represent virtual, virtual, and modify methods.
When modifying the method:
This is called a virtual method. A virtual method indicates that this method is virtual. This method may not be implemented and can be overwritten.
The key phrase is: This method can be rewritten.
This means that if this method is to be overwritten and override, it must be a virtual method, because the abstract modifier method is an implicit virtual method, so abstract and virtual methods can be override.
The override keyword indicates rewriting, And the modifier is method.
The override method provides a new implementation of Members inherited from the base class. The method declared by override is called the override base method. The override base method must have the same signature as the override method.
When modifying the Method:
1: You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override.
The override and virtual methods must have the same access level modifier.
2: you cannot use the new, static, or virtual modifier to modify the override method.
3: The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the overwritten attribute must
Is virtual, abstract, or override.
The new Keyword indicates hiding, and the modifier is method.
The difference between new and override is that new is to hide the parent class method, which is like telling others that this method and the parent class method are two different methods, however, their signatures are exactly the same, while override is different. override tells others that my method will be called using my instance in the future, and the method of the parent class will be called using the instance of the parent class.
Summary: Abstract, virtual, override, and new relationships.
The override method must be abstract, virtual, or override.
The abstract method is an implicit virtual method.
The virtual method indicates that this method can be overwritten. Of course, you can also not override it.
Abstract method indicates that this method must be overwritten.
The new method indicates that this method has nothing to do with the parent class. It is a new "new" method, but the signature is just the same.
Last question:Copy codeThe Code is as follows: class
{
Public virtual void F ()
{
Console. WriteLine ("A.F ");
}
}
Class B:
{
Public override void F ()
{
Console. WriteLine ("B. F ");
}
}
Class C: B
{
New public virtual void F () {Console. WriteLine ("C. F ");}
}
Class D: C
{
Public override void F () {Console. WriteLine ("D. F ");}
}
Class Program2
{
Static void Main ()
{
D d = new D ();
A a = d; B B = d;
C c = d; a. F (); B. F ();
C. F ();
D. F ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.