Page 79 - CSharp/C#
P. 79

private class Baz
             {
                 public string Value { get; set; }
             }

             public void Do()
             {
                 var baz = new Baz { Value = 42 };
             }
         }

         public class Bar()
         {
             public Bar()
             {
                 var myInstance = new Foo();

                 // Compile Error - not accessible due to private modifier
                 var someValue = foo.someProperty;
                 // Compile Error - not accessible due to private modifier
                 var baz = new Foo.Baz();
             }
         }


        internal


        The internal keyword makes a class (including nested classes), property, method or field available
        to every consumer in the same assembly:


         internal class Foo
         {
             internal string SomeProperty {get; set;}
         }

         internal class Bar
         {
             var myInstance = new Foo();
             internal string SomeField = foo.SomeProperty;

             internal class Baz
             {
                 private string blah;
                 public int N { get; set; }
             }
         }


        This can be broken to allow a testing assembly to access the code via adding code to
        AssemblyInfo.cs file:


         using System.Runtime.CompilerServices;

         [assembly:InternalsVisibleTo("MyTests")]



        protected


        The protected keyword marks field, methods properties and nested classes for use inside the


        https://riptutorial.com/                                                                               25
   74   75   76   77   78   79   80   81   82   83   84