4 accessors and 8 modifiers in C # in a detailed

Source: Internet
Author: User
Tags modifiers

4 access Modifiers (keywords that are added to a class, struct, or member declaration)

Public: The access modifier for the type and type member. There are no restrictions on their access.

Internal: Internal, is the access modifier of type and type member. All classes in the same assembly can be accessed

Private: is a member access modifier. can only be accessed in the classes and structs in which they are declared.

Protected:: Protected, is a member access modifier. can only be accessed in its class and its derived classes.

Protected internal: Access level is internal or protected. That is, "all classes in the same assembly, as well as subclasses in all assemblies, can access

Note the point:

A member or type can have only one access modifier, except when using the protected internal combination.

If an access modifier is not specified in the member declaration, the default accessibility is used

Default accessibility of type members

Belong to

Default Member Accessibility

The accessibility of the claims allowed by this member

Enum

Public

No

Class

Private

Public

Protected

Internal

Private

protected internal

Interface

Public

No

struct

Private

Public

Internal

Private

Internal and protected Internal explanation (i.e. what is the same assembly)

Example 1: (Here the same assembly refers to the same namespace)

CLASS1:

Using System;

Using System.Collections.Generic;

Using System.Text;

Namespace Example05lib

{

public class Class1

{

Internal String strinternal = null;

Public String strpublic;

Internal protected String strinternalprotected = null;

}

}

CLASS2:

Using System; Using System.Collections.Generic; Using System.Text

Namespace Example05lib {class Class2 {private String tmpstr=new Class1 (). strinternal;    Private String tmpstr=new Class1 (). strinternalprotected;  Private String tmpstr=new Class1 (). Strpublic; } }

1.1 Results: The Class2 class can access the strinternal members of Class1, and of course can also access the strinternalprotected members, because they are in the same assembly

CLASS3:

Using System; Using System.Collections.Generic; Using System.Text using Example05lib

Namespace Example05 {class Program {class Class3:class1 {public Class3 () { Base.              strinternalprotected;           Base.strpublic; }         }   } }

1.2 Result: The CLASS3 class cannot access the strinternal members of the CLASS1 because they are not in the same assembly. However, it is possible to access the strinternalprotected member because CLASS3 is the inheriting class of Class1.

Using System; Using System.Collections.Generic;       Using System.Text using Example05lib namespace Example05 {class Program {static void Main (string[] args) {     String tmpstr=new Class1 (). Strpublic; } } }

1.3 Result: Unable to access strinternalprotected and strinternal members because they are neither in the same assembly nor are there inheritance relationships

Example 2 (where the same assembly refers to the same. cs file, the namespace of a different. cs file is omitted)

2.1 The sample consists of two files: Assembly1.cs and Assembly2.cs. The first file contains an internal base class BaseClass. In the second file, an attempt to instantiate BaseClass will produce an error.

Internal class BaseClass

{

public static int intm = 0;

}

Class Testaccess

{

static void Main ()

{

BaseClass myBase = new BaseClass (); CS0122

}

}

2.2 Use the same file as the file used in Example 1 and change the accessibility level of baseclass to public. The accessibility level of member Intm is also changed to internal. In this example, you can instantiate a class, but you cannot access internal members.

public class BaseClass

{

internal static int intm = 0;

}

public class Testaccess

{

static void Main ()

{

BaseClass myBase = new BaseClass (); Ok.

Baseclass.intm = 444; CS0117

}

}

Example 3

For some large projects, usually composed of many DLL files, referencing these DLLs, you can access the DLL inside the class and the method inside the class. For example, you write a log DLL, any project as long as the reference to this DLL can realize the function of logging, the DLL file program is an assembly.

If the assembly that you log is so defined

Namespace Logerhelper

{

Internal Class AA

{

public void BB ()

{

Return "";

}

}

public class Write

{

public void Writein (string content)

{

Class X = new AA ();

X.BB ();

}

}

}

When another project references this DLL

It can be accessed so

Logerhelper.write x = new Logerhelper.write ();

X.writein ("");

But you can't access this.

Logerhelper.aa x = new Logerhelper.aa ();

X.BB ();

This is called, and can only be accessed in the Assembly

8 Declaration Modifiers

Partial: Define partial classes and structures throughout the same assembly, as described in question 1.

Static: Declares a member that belongs to the type itself and not to a particular object.

Abstract: A class that can only be a base class of other classes. A method in a class declares only that it is not implemented, and the implementation of the method is done in his derived class.

Sealed: The specified class cannot be inherited.

Virtual: Used to decorate a method, property, indexer, or event declaration, and to allow these objects to be overridden in a derived class

Override: Provides a new implementation of a member inherited from a base class

New: As a modifier to hide members inherited from a base class member, hiding a member without using the new modifier is allowed, but generates a warning. As an operator for creating objects and calling constructors.

Extern: Used to declare methods that are implemented externally. The common use of the extern modifier is to use the DllImport attribute with the Interop service when it is being transferred into unmanaged code. In this case, the method must also be declared as static

Virtual , the difference between override and new

1. Virtual and override are used. The virtual method method () is declared in base class base and modified by virtual, and overridden in subclass derived by the method () and the override. So when an instance of a subclass is assigned to an object of the base class (no coercion is required), base bclass= new Derived (); Bclass.method () is called the method () of the subclass, not the base class. So

2. New does not need to be used with virtual companion. Methods method () is declared in base class base, a method () with the same name is declared in subclass derived and decorated with a new. Then when an instance of a subclass is assigned to an object of the base class, base bclass= new Derived (), Bclass.method () is called the method () methods of the base class class, not the subclass.

3. This shows that override can override the method of the base class so that the method of the base class is implemented with the contents of the subclass, and new does not overwrite the method of the base class, but rather a new method that defines a subclass, which belongs only to the subclass, regardless of the method of the base class, but only the name.

Below, I use examples to illustrate the subtle differences between them:

public class grandclass//base class {public Grandclass () {Console.WriteLine ("in Grandclass.const         Ructor ");  The public virtual void Method ()//is used by virtual to override in subclasses, and new does not need to be {Console.WriteLine ("in         Grandclass.method () "); } }
Public class parentclass:grandclass//inherits the base class and looks at the override state {public ParentClass () {CONSOLE.WR         Iteline ("in Parentclass.constructor"); } public override void Method ()//Use override to say that the methods of the base class are redefined {Console.WriteLine ("in Parentcla Ss.         Method () use override "); } }
Public class newparentclass:grandclass//inherits the base class and looks at the new state {public newparentclass () {CONSOLE.W         Riteline ("in Newparentclass.constructor"); The new public void Method ()//uses new, rather than using the methods of the base class, to redefine a subclass method, except that the method name is the same as the base class {Console.writelin         E ("in Newparentclass.method ()"); } }

The following calling code:

static void Main () {Grandclass parent= (grandclass) new ParentClass ();//Use the override subclass to frame a base class object handle Parent.metho         D ();         Grandclass newparent= (Grandclass) new Newparentclass ();//Use the new subclass to frame a base class object handle Newparent.method (); Newparentclass newparent1=new Newparentclass ();//a sub-class handle Newparent1.method (); }

The result is this:

[1]in grandclass.constructor [2]in parentclass.constructor [3]in parentclass.method ()  use  override [4]in grandclass.constructor [5]in newparentclass.constructor [6]In  Grandclass.method () [7]in grandclass.constructor [8]in newparentclass.constructor [9]In  Newparentclass.method ()

I added the serial number before the result. For the following analysis: [1],[2] Two sentences are Grandclass parent= (grandclass) new ParentClass ();   (Note the initialization sequence of the class builder and the base class builder all of a sudden)   [3] is parent.method (); results.   [4],[5] Two sentences are the result of Grandclass newparent= (grandclass) new Newparentclass ();   [6] is the result of Newparent.method ();   [7],[8] Two sentences are the result of Grandclass newparent1= (grandclass) new Newparentclass (); [9] is the result of Newparent1.method ();
As we can see here, we also construct a base class handle with the object of the subclass. The result is obvious, and you can see the difference between [3] and [6].   [3] The method () of the subclass was called, and [6] called the method () of the base class.      This means that override can override the method of the base class so that the method of the base class is implemented with the contents of the subclass, and new does not overwrite the method of the base class, but rather a new method that defines a subclass, which belongs only to the subclass, regardless of the method of the base class, except the name.   The basis of this example is the creation of a subclass object framed into a base class object, the purpose is to implement using the base class handle to call the subclass method, in order to achieve the overloaded polymorphism.   If you want to invoke the new method of a subclass, call it with a handle to the subclass (absolutely not with the base class handle). The result [9] can be seen. With new, when defining a method name for a subclass, there is no way to define the method name in the same way as the base class, but this method works only in subclasses, not on the base class. That is, the new method is the newly defined method of the subclass. Override is a straight-forward overload.

Note: Some content reproduced: http://blog.csdn.net/susan19890313/article/details/7575228

4 accessors and 8 modifiers in C # in a detailed

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.