C# 7.2 introduced new access modifier – ‘Private Protected’ – How it works?

Before Private Protected C# had 5 access modifier such as public, private, protected, internal, protected internal.

As per MSDN

Finally, a new compound access modifier: private protected indicates that a member may be accessed by containing class or derived classes that are declared in the same assembly. While protected internal allows access by derived classes or classes that are in the same assembly, private protected limits access to derived types declared in the same assembly.

This private protected new compound access modifier is used to overcome the limitation of protected internal. In private protected access modifier, the derived class from the different assembly cannot access the Methods, Fields and Properties.

Code Example

Protected Internal in C#

Private Protected in C# – New in C# 7.2

Please check the difference in line number 21 in both the above code.


All Access Modifiers in C#

public: Access is not restricted.

protected: Access is limited to the containing class or types derived from the containing class.

internal: Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private: Access is limited to the containing type.

private protected: Access is limited to the containing class or types derived from the containing class within the current assembly.

Moving file from one destination to another destination in C# using System.IO.File Class

Step 1: Import System.IO class
Step 2: Create a test file in your local system.
Step 3: Give Source and Destination Path, which should be available on your system.
Step 4: Combine Path with File name using System.IO.Path class. You can also using basic string concordinate.
Step 5: Check if Direct is exist before moving the file.
Step 6: Use File.Copy static method to move the file.

Note: If you have any queries, please write in below comment box.

Copying file from one destination to another destination in C# using System.IO.File Class

Step 1: Import System.IO class
Step 2: Create a test file in your local system.
Step 3: Give Source and Destination Path, which should be available on your system.
Step 4: Combine Path with File name using System.IO.Path class. You can also using basic string concordinate.
Step 5: Check if Direct is exist before moving the file.
Step 6: Use File.Copy static method to move the file.

Note: If you have any queries, please write in below comment box.