1 00:00:07,260 --> 00:00:07,710 Hello. 2 00:00:08,430 --> 00:00:11,970 In this video, we're going to talk about Question 28. 3 00:00:12,240 --> 00:00:13,890 What are indexers? 4 00:00:14,310 --> 00:00:19,770 Indexers are something we use all the time, usually without giving it much thought. 5 00:00:20,220 --> 00:00:27,180 Whenever accessing an element under a specific index of our east, we are actually calling this index 6 00:00:27,180 --> 00:00:27,480 are. 7 00:00:31,920 --> 00:00:36,180 This looks exactly the same as accessing the first element of an array. 8 00:00:40,550 --> 00:00:47,660 We fleeced using the indexer looks extremely simple, but there's a lot going on in the last class whenever 9 00:00:47,660 --> 00:00:49,130 we use its indexer. 10 00:00:49,430 --> 00:00:52,100 Let's take a look into its source code. 11 00:00:53,780 --> 00:01:00,530 We can recognize that this is the definition of the index, her by this part, that this keyword and 12 00:01:00,530 --> 00:01:04,270 the parameter in brackets for the case of the least. 13 00:01:04,370 --> 00:01:11,710 The index is quite complex, but in the end its job is to get or set the value of the internal error. 14 00:01:12,140 --> 00:01:13,400 That's the least holds. 15 00:01:13,790 --> 00:01:21,950 We learned about it in the what is at least lecture indexers don't necessarily use integers as barometers. 16 00:01:23,240 --> 00:01:29,810 For example, when using dictionaries, we use indexers to access another month under a given key and 17 00:01:29,810 --> 00:01:32,690 the key of the dictionary can be whatever we want. 18 00:01:32,990 --> 00:01:39,500 For example, if the key of the dictionary is a string, then the parameter of the index R will also 19 00:01:39,500 --> 00:01:40,250 be a string. 20 00:01:48,390 --> 00:01:52,530 We can define our own indexers in the types we create. 21 00:01:52,920 --> 00:01:57,030 Let's define a simple class that works as a wrapper for our. 22 00:02:22,010 --> 00:02:28,760 For now, when trying to use an indexer on an object of this class, we get a compilation error because 23 00:02:28,760 --> 00:02:33,890 this class does not support it to make it work in the mildest class. 24 00:02:34,010 --> 00:02:37,240 We must define an index are accepting and. 25 00:02:49,230 --> 00:02:56,850 On Get, this index simply retrieves the value from the array and onset it overwrites it with the provided 26 00:02:56,850 --> 00:02:57,390 value. 27 00:02:58,170 --> 00:03:00,840 As we can see, this gold now compiles. 28 00:03:01,470 --> 00:03:08,640 We can define Usman indexers as we want if they only differ by the types or account of parameters. 29 00:03:08,940 --> 00:03:14,670 For example, I can add an index are accepting a string to the my wrist glass. 30 00:03:23,870 --> 00:03:26,300 I could use this indexer like this. 31 00:03:31,360 --> 00:03:35,170 Indexers with multiple parameters are also allowed. 32 00:03:42,090 --> 00:03:48,690 Please note that it is also possible to have an indexer, we've got there only or we sat there only, 33 00:03:48,840 --> 00:03:50,520 but this is more unusual. 34 00:03:53,060 --> 00:03:59,630 In this case, will be able to access an element of a given index, but we won't be able to overwrite 35 00:03:59,630 --> 00:03:59,960 it. 36 00:04:08,600 --> 00:04:14,840 As you can see, accessing the element works, but certainly it does not, because the index are taking 37 00:04:14,840 --> 00:04:15,860 two parameters. 38 00:04:16,130 --> 00:04:17,600 It does not have us later. 39 00:04:18,230 --> 00:04:23,270 Let's sum up indexers allow instances of a type to be indexed. 40 00:04:23,450 --> 00:04:30,680 Just like all, index cards are most often used with types representing corrections, but we can add 41 00:04:30,680 --> 00:04:32,000 them to any type. 42 00:04:32,330 --> 00:04:38,380 Indexes are simple and not too are to use for developers, and we should consider adding them to our 43 00:04:38,390 --> 00:04:44,000 types, especially if we already have some methods like good value at index. 44 00:04:44,390 --> 00:04:49,490 In this case, we should definitely consider refactoring and introducing an index. 45 00:04:50,420 --> 00:04:56,630 During the interview, you can be asked if it's possible to have a class with an index are accepting 46 00:04:56,630 --> 00:04:58,370 a string as a parameter. 47 00:04:58,850 --> 00:05:00,200 Well, of course it is. 48 00:05:00,410 --> 00:05:03,450 We can define index cards within the parameters. 49 00:05:04,010 --> 00:05:08,270 And can we have more than one index are defined in the class? 50 00:05:08,870 --> 00:05:09,440 Yes. 51 00:05:09,770 --> 00:05:16,010 Just like with method overloading, we can have as many indexes as we want, as long as they differ 52 00:05:16,010 --> 00:05:19,670 by the type count or order of parameters. 53 00:05:20,390 --> 00:05:23,060 All right, that's it about indexers. 54 00:05:23,360 --> 00:05:26,270 Thanks for watching and see you in the next video.