Indexers, Partial Class And Partial Method


Published on

This PDF is related to Rajpat Systems Part. This PDF contains Indexers in C#, Partial Classes in C#, Partial Method in C#,

"Knowledge Is Free Lets Share It"

Published in: Technology, Sports
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Indexers, Partial Class And Partial Method

  1. 1. What Is an Indexer?
  2. 2. • An indexer is a set of get and set accessors, similar to those of properties
  3. 3. Indexers and PropertiesIndexers and properties are similar in many ways.• • Like a property, an indexer does not allocate memory for storage.• • Both indexers and properties are used primarily for giving access to other data members with which they are associated, and for which they provide set and get access. – – A property is usually accessing a single data member. – – An indexer is usually accessing multiple data members.
  4. 4. Some additional points• • An indexer can have either one or both of the accessors.• • Indexers are always instance members; hence, an indexer cannot be declared static.• • Like properties, the code implementing the get and set accessors does not have to be• associated with any fields or properties. The code can do anything, or nothing, as long as the get accessor returns some value of the specified type.
  5. 5. Declaring an Indexer• • An indexer does not have a name. In place of the name is the keyword this.• • The parameter list is between square brackets.• • There must be at least one parameter declaration in the parameter list.
  6. 6. Declaring an Indexer
  7. 7. Comparing an indexer declaration to a property declaration
  8. 8. The set AccessorWhen the indexer is the target of an assignment, the set accessor is called, and receives two items of data. – • An implicit parameter, named value, where value holds the data to be stored. – • One or more index parameters that represent where it should be stored.
  9. 9. The get Accessor• When the indexer is used to retrieve a value, the get accessor is called with one or more index parameters.• The code in the get accessor body must examine the index parameters, determine which field they represent, and return the value of that field.
  10. 10. • • It has the same parameter list as in the indexer declaration.• • It returns a value of the same type as the indexer.
  11. 11. More About IndexersAs with properties, the get and set accessors cannot be called explicitly. Instead, the get accessor is called automatically when the indexer is used in an expression for a value. The set accessor is called automatically when the indexer is assigned a value with the assignment statement.
  12. 12. • class Class1 • Temp1 = value; // Note the implicit• { variable "value".• int Temp0; // Private field • }• int Temp1; // Private field • }• public int this [ int index ] // The indexer • }• { • class Example• get • {• { • static void Main()• return ( 0 == index ) // Return value of • { either Temp0 or Temp1 • Class1 a = new Class1();• ? Temp0 • Console.WriteLine("Values -- T0: {0}, T1:• : Temp1; {1}", a[0], a[1]);• } • a[0] = 15;• set • a[1] = 20;• { • Console.WriteLine("Values -- T0: {0}, T1:• if( 0 == index ) {1}", a[0], a[1]);• Temp0 = value; // Note the implicit • } variable "value". • }• else
  13. 13. Access Modifiers on AccessorsThere are several restrictions on the access modifiers of accessors. – • An accessor can have an access modifier only if the member (property or indexer) has both a get accessor and a set accessor. – • Although both accessors must be present, only one of them can have an access modifier. – • The access modifier of the accessor must be strictly more restrictive than the access level of the member.
  14. 14. Hierarchy of strictly restrictive accessor levels
  15. 15. Indexer Overloading• class MyClass• {• public string this [ int index ]• {• get { ... }• set { ... }• }• public string this [ int index1, int index2 ]• {• get { ... }• set { ... }• }• public int this [ float index1 ]• {• get { ... }• set { ... }• }• ...• }
  16. 16. Partial Classes and Partial Types
  17. 17. Partial Class• The declaration of a class can be partitioned among several partial class declarations. – • Each of the partial class declarations contains the declarations of some of the class members. – • The partial class declarations of a class can be in the same file or in different files.
  18. 18. • Type modifier• partial class MyPartClass // Same class name as following• {• member1 declaration• member2 declaration• ...• }• Type modifier• partial class MyPartClass // Same class name as preceding• {• member3 declaration• member4 declaration• ...• }
  19. 19. • All the partial class declarations comprising a class must be compiled together.• A class using partial class declarations has the same meaning as if all the class members were declared within a single class declaration body.• Besides classes, you can also create two other partial types: – • Partial structs. – • Partial interfaces.
  20. 20. Partial Methods• Partial methods are methods that are declared in two parts in a partial class.• The two parts of the partial method are: – • The defining partial method declaration gives the signature and return type, and the implementation part of the declaration consists of only a semicolon. – • The implementing partial method declaration gives the signature, return type, and also the implementation in the normal format of a statement block.
  21. 21. The important things to know• • Both the defining and implementing declaration must match in signature and return type. The signature and return type have the following characteristics: – – The contextual keyword partial must be included in both the defining and implementing declarations immediately before the keyword void. – – The signature cannot include access modifiers⎯making partial methods implicitly private. – – The return type must be void. – – The parameter list cannot contain out parameters.• • You can have a defining partial method without an implementing partial method. In this case, the compiler removes the declaration and any calls to the method made inside the class. If, however, the class has an implementing partial method, it must also have a defining partial method.