Prep Access Specifiers & Accessors

  1. What is the difference between an access specifier and an accessor?
    An access specifier refers to the accessibility of an object or method, in C# there are five: private, protected, internal, protected internal, and public. All types and type members have an accessibility level. The accessibility level controls whether they can be used from other code in your assembly or other assemblies. 

    An accessor is a property of a class, allowing us to access a field associated with that accessor using the get() or set() values. It is declare like this: public int Id { get; set; }
  2. What's the difference between a field and a property?
    A field represents an internal variable associated with an object type (ie, Id will likely be a int and only declared locally). 

    A property is a method (accessor) whereby we access that field externally using the { get; set; } code.
  3. Define private and public access specifiers.
    Private means we can only access the item within the same class or struct

    The Public type or member can be accessed by any other code in the same assembly (project) or another assembly (project) that references it. Usually a single solution contains multiple projects, a public variable, if the project was referenced, would allow us to see that variable.
  4. Define the protected access specifier. Elaborate.
    Protected means we can view the item within the same class but also view the item in a derived class (aka subclass or child class).

    That is, we can't access in our typical Main() class... but if we declare a protected variable in one class, and then in a second class we inherit the first class, then we could see this variable there as well as its declared class.
  5. Define the internal access specifier. Elaborate.
    The Internal specifier can be accessed by any code in the same assembly, but not from another assembly.

    Each project, when compiled, produces its own assembly. So we can also say that the Internal specifier is accessible to all code within the same class as well as anywhere within that same project and namespace. Another project within the same solution could not access this item, even if the project was referenced.
  6. Define the protected internal access specifier. Elaborate.
    The protected internal specifier allows the item to be accessible using either the protected OR internal specifier rules. That is, it is accessible within the same assembly (but not other assemblies. like the internal specifier) or by a derived classes within the same assembly (like the protected specifier).
  7. Why do we use access modifiers?
    We use them to hide the details of how something is done-that is, to create a layer of abstraction which also aids in decoupling code and making it more maintainable because every public method, if changed, can impact other classes down the hierarchy.
  8. Declare a Name accessor which has a public get and a private set. Why would we create this?
    public string Name { get; private set; }

    Doing this allows anyone to use it (public) while also allowing the class to initialize it, preventing others from modifying it externally from the class.
  9. How do we declare an accessor TotalDownloads to have a default level of 5?
    public int TotalDownloads { get; set; } = 5;
  10. How would you write an accessor (property) as read-only? write-only? both?
    A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property that has both accessors is read-write.
  11. Write a simple Person class which contains a single _name field with a private set method and a public get method. Use shorthand version and a longhand version.
    • class Person
    • {
    •   private string _name; // the name field
    •   public string Name // the Name property
    •   {
    •     get => _name;
    •     set => _name = value;
    •   }
    • }

    • public Person
    • {
    •   public string Name { get; private set; }
    • }
Card Set
Prep Access Specifiers & Accessors
Questions on access specifiers (private, protected, internal, public, protected internal) and accessors (get, set)..