Parse the private protected access modifier in Visual C #7.2,

Source: Internet
Author: User

Parse the private protected access modifier in Visual C #7.2,

In March last December, with the release of Visual Studio 2017 Update 15.5, Visual C # ushered in its latest version: 7.2. in this version, a new feature that is hard to understand is the private protected Access Modifier (Access Modifier ). At this point, the access modifier for C # has the following types:

  • Private
  • Protected
  • Public
  • Internal
  • Internal protected
  • Private protected

Since private and protected are available, what is private protected? What does it have to do with internal protected? This article briefly introduces.

What is private protected?

Before explaining private protected, let's first review the internal protected access modifier. Internal protected indicates that other types in the same Assembly, or subclasses of the current class, have the ability to access the internal protected members in this class, which can be expressed:

Public sealed class SerializationHelper {public string Serialze (object s) {using (var memoryStream = new MemoryStream () {var serializer = new XmlSerializer (s. getType (); serializer. serialize (memoryStream, s); return Encoding. UTF8.GetString (memoryStream. toArray () ;}} public abstract class DataStorage {private readonly SerializationHelper serializer = new SerializationHelper (); protected SerializationHelper Serializer => serializer; protected abstract void SaveObject (object obj );} public sealed class InMemoryDataStorage: DataStorage {private readonly List <string> serializedData = new List <string> (); protected override void SaveObject (object obj) => serializedData. add (Serializer. serialze (obj ));}

In the above Code, SerializationHelper provides a mechanism to serialize objects into XML strings. DataStorage is the base class for all object data storage, of course, it also provides a method for its subclass to access the object serializer. Because this object Serializer is provided to its subclass for calling, the Serializer attribute in DataStorage is protected. Finally, InMemoryDataStorage inherits DataStorage and implements the SaveObject method by calling the Serializer attribute provided by the base class.

Of course there is no problem with the entire implementation. However, by reviewing the visibility of all types, we found that we do not intend to expose the SerializationHelper class to the outside world, that is, we do not want other assemblies to directly access the SerializationHelper class. Therefore, we set it to internal. That is:

internal sealed class SerializationHelper{    public string Serialze(object s)    {        using (var memoryStream = new MemoryStream())        {            var serializer = new XmlSerializer(s.GetType());            serializer.Serialize(memoryStream, s);            return Encoding.UTF8.GetString(memoryStream.ToArray());        }    }}

Okay, the problem is that the compiler started to complain, saying that the access level of the SerializationHelper class is lower than that of the DataStorage. Serializer attribute:

Internal sealed class SerializationHelper {public string Serialze (object s) {using (var memoryStream = new MemoryStream () {var serializer = new XmlSerializer (s. getType (); serializer. serialize (memoryStream, s); return Encoding. UTF8.GetString (memoryStream. toArray () ;}} public abstract class DataStorage {private readonly SerializationHelper serializer = new SerializationHelper (); private protected SerializationHelper Serializer => serializer; protected abstract void SaveObject (object obj);} public sealed class InMemoryDataStorage: DataStorage {private readonly List <string> serializedData = new List <string> (); protected override void SaveObject (object obj) => serializedData. add (Serializer. serialze (obj ));}

However, once the private protected access modifier is used, the DataStorage. Serializer attribute can only be accessed in the subclass of the assembly where DataStorage is located.

How to use private protected

The private protected access modifier is a new feature of C #7.2. Since the use of the Roslyn Compiler service C #6.0, the C # compiler version update can be separated from the release of. NET Framework and Visual Studio. This is gradually apparent in the release of C # 7.x( including 7.1 and 7.2. In Visual Studio 2017's advanced compilation options, developers can easily select the required C # version:

As described in, right-click the C # project, and on the Build tab of the project properties, click the Advanced button. In the Advanced Build Settings dialog box, select the required C # Language version from the Language version drop-down list. Where:

  • C # latest major version (default): C # The latest main version, that is, the major version released with Visual Studio, corresponds to C #7.0 on VS2017
  • C # latest minor version (latest): the latest version of C #, usually obtained through the VS2017 upgrade package

To use the private protected access modifier, select C # latest minor version (latest) or C #7.2 here.

Summary

This article analyzes the new feature of C #7.2: private protected access modifier, and uses cases to describe its application scenarios and support for new features of C # in Visual Studio 2017.

Related Article

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.