this.md - brainchildservices/curriculum GitHub Wiki

  1. Define this keyword.

    • In C#, ‘this’ keyword is used to refer to instance members of the current class from within an instance method or a constructor.
  2. Why this keyword works only for non-static members.

    • In C#, ‘this’ pointer works only for nonstatic members of the class because ‘this’ works on the current instance and nonstatic members can be accessed by the instance of the class.
  3. Define uses of this keyword.

    • Use of ‘this’ keyword is that we can use it to declare indexers. To distinguish the method parameters from the class fields when they have the same name. To call the method in the same class. Use ‘this’ keyword to call a constructor in the same class.
  4. How to pass object as argument into method?

    • We can pass primitive (basic) data types in methods as arguments. Similarly, we can pass objects in methods too.
  5. What is an indexer?

    • C# indexers are usually known as smart arrays.A C# indexer is a class property that allows you to access a member variable of a class using the features of an array.
  6. How are indexers created?

    • In C#, indexers are created using this keyword.
  7. Differentiate between indexers and properties.

      Properties 
          Properties in C# are named members that use access modifiers to set and retrieve values of fields declared in a secured manner.
          Properties are used for abstracting and encapsulating access to a field of a class by defining only important actions and hiding their 
          implementation.
          Properties are invoked through a described name and can be declared as a static or an instance member.
      Indexers
          Indexers in C# are data members that act as an array and allow you to access data within objects to be indexed in the same way.
          Indexers are always declared as instance members, never as static members. 
          Indexers are implemented in the same way as properties, except that the declaration of an indexer must have at least one parameter.
    
  8. How are indexers declared?

    • Indexers are always declared as instance members, never as static members.
  9. Define advantages of indexers.

    Advantages of Indexers: * They are used for overloading an [ ] operator. * The syntax is simple an easy to user. * It supports overloading for any user defines array in C# * The access specifiers simply the code complexity.
  10. Give a reason why we need a this keyword?

    • The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.