Page 78 - CSharp/C#
P. 78

Chapter 3: Access Modifiers




        Remarks



        If the access modifier is omitted,

            •  classes are by default internal
            •  methods are by deault private
            •  getters and setters inherit the modifier of the property, by default this is private


        Access modifiers on setters or getters of properties can only restrict access, not widen it: public
        string someProperty {get; private set;}

        Examples



        public


        The public keyword makes a class (including nested classes), property, method or field available
        to every consumer:


         public class Foo()
         {
             public string SomeProperty { get; set; }

             public class Baz
             {
                 public int Value { get; set; }
             }
         }

         public class Bar()
         {
             public Bar()
             {
                 var myInstance = new Foo();
                 var someValue = foo.SomeProperty;
                 var myNestedInstance = new Foo.Baz();
                 var otherValue = myNestedInstance.Value;
             }
         }


        private


        The private keyword marks properties, methods, fields and nested classes for use inside the class
        only:


         public class Foo()
         {
             private string someProperty { get; set; }





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