1 00:00:06,780 --> 00:00:07,170 Hello. 2 00:00:08,410 --> 00:00:14,340 In this lecture will focus on Question 35, What are anonymous types? 3 00:00:14,970 --> 00:00:18,240 Anonymous types are types without names. 4 00:00:18,720 --> 00:00:27,240 Anonymous types provide a convenient way of encapsulating a set of only properties into a single object 5 00:00:27,300 --> 00:00:31,230 without having to explicitly define the type first. 6 00:00:47,780 --> 00:00:54,850 As you can see, to create an object of an anonymous type, we simply use the new keyword and then put 7 00:00:54,860 --> 00:00:57,460 on the properties we want in the braces. 8 00:00:57,890 --> 00:01:05,720 Here we created an anonymous type with free properties, name and city of type, string and age of type 9 00:01:05,720 --> 00:01:13,400 int the properties of anonymous types are read only, so the code modifying them will not compile. 10 00:01:16,920 --> 00:01:23,760 To understand better what may be the use case for anonymous types, let's consider a simple coding challenge. 11 00:01:24,150 --> 00:01:26,850 First, let's define a collection of parts. 12 00:01:28,240 --> 00:01:31,480 Each bird has a name type and wait. 13 00:01:32,110 --> 00:01:39,760 What we want to do is to build a collection of strings that will contain data about each type, including 14 00:01:39,760 --> 00:01:42,790 the average weight of pets of this type. 15 00:01:43,300 --> 00:01:46,330 The result should be sorted by ascending. 16 00:01:46,570 --> 00:01:52,030 In other words, it should look like this will use Link to do it. 17 00:01:52,540 --> 00:01:58,930 If you don't know Link and you would like to learn it took Hold My Course Link Tutorial Master the key 18 00:01:58,960 --> 00:02:04,330 C-sharp library in the last lecture of this course, you can find a discount coupon. 19 00:02:04,900 --> 00:02:05,560 All right. 20 00:02:05,650 --> 00:02:08,890 First, we need to group the pets by type. 21 00:02:15,490 --> 00:02:19,570 For each of the groups, we want to calculate the average weight. 22 00:02:27,350 --> 00:02:30,320 Well, this works fine, but there is one problem. 23 00:02:30,770 --> 00:02:37,310 We only selected the average weights of each group now, but we lost the information about the name 24 00:02:37,430 --> 00:02:38,720 of each of those groups. 25 00:02:39,170 --> 00:02:47,540 We must change this cult to not select votes as the average weight is afloat, but pairs of prototype 26 00:02:47,540 --> 00:02:48,230 cells. 27 00:02:48,590 --> 00:02:51,020 But how should we represent those powers? 28 00:02:51,530 --> 00:02:55,340 We could define a class struct or a record for it. 29 00:03:10,200 --> 00:03:13,800 Again, this works, but it seems like a big effort. 30 00:03:14,280 --> 00:03:21,360 We created a whole separate type for this very specific piece of data will probably never use it in 31 00:03:21,360 --> 00:03:26,130 a different context, not to mention that this name is a bit awkward. 32 00:03:26,340 --> 00:03:28,020 But how else could we call it? 33 00:03:28,410 --> 00:03:32,490 There is really no good name for this very specific set of data. 34 00:03:33,210 --> 00:03:36,270 The solution is to use an anonymous type. 35 00:03:37,050 --> 00:03:42,780 Anonymous type is a type defined right where it's needed without even giving it a name. 36 00:03:43,170 --> 00:03:50,340 It's perfect for use cases like ours when the type is small and temporary and we don't intend to use 37 00:03:50,340 --> 00:03:51,630 it anywhere else. 38 00:04:00,880 --> 00:04:01,550 All right. 39 00:04:02,020 --> 00:04:09,640 So now this anonymous object will hold a prototype and a float representing the average weight of births 40 00:04:09,640 --> 00:04:10,600 of this type. 41 00:04:11,020 --> 00:04:16,480 The only thing left to do is to order this data and make it to some readable strings. 42 00:04:31,270 --> 00:04:33,520 Let's make sure this works as expected. 43 00:04:39,110 --> 00:04:39,800 All right. 44 00:04:40,080 --> 00:04:44,030 Seems like our code using anonymous type works correctly. 45 00:04:45,170 --> 00:04:51,740 Let's take another look at this type because it doesn't even have a name will not be able to use it 46 00:04:51,740 --> 00:04:56,720 anywhere else because how could we refer to it if we don't know its name? 47 00:04:57,500 --> 00:05:03,170 Actually, the compiler gives it a name that can be seen in the common intermediate language. 48 00:05:03,260 --> 00:05:10,550 But even if we use it doesn't to find it, it won't be possible to use it just to satisfy your curiosity. 49 00:05:10,700 --> 00:05:14,660 I joked how the compiler named this particular type. 50 00:05:15,410 --> 00:05:18,440 He was the name of the anonymous type we defined. 51 00:05:18,890 --> 00:05:21,050 As you can see, it's not very readable. 52 00:05:21,410 --> 00:05:27,770 Please note that from the perspective of the common language runtime, anonymous types are no different 53 00:05:27,860 --> 00:05:29,480 than other types. 54 00:05:29,980 --> 00:05:34,470 It's least the most important information about anonymous types. 55 00:05:34,850 --> 00:05:37,670 They contain only root only properties. 56 00:05:38,120 --> 00:05:43,100 No other kinds of class members, such as methods or events, are valid. 57 00:05:43,610 --> 00:05:49,580 If no names are given to the properties of the anonymous type, the compiler will use the name of the 58 00:05:49,580 --> 00:05:54,230 property that was used to set the value of the anonymous Typekit property. 59 00:05:54,590 --> 00:05:58,340 For example, if instead of this, we will do this. 60 00:05:59,940 --> 00:06:08,190 The name of the first property would be key, the same as the name of the property assigned to it when 61 00:06:08,190 --> 00:06:11,250 this assigned value is not a property or a field. 62 00:06:11,370 --> 00:06:14,160 It must be given a name, for example. 63 00:06:14,170 --> 00:06:21,480 In this case, it doesn't work because this value is calculated and it doesn't have a name. 64 00:06:21,870 --> 00:06:23,560 We must give it a name. 65 00:06:23,670 --> 00:06:26,280 Otherwise, this error will be seen. 66 00:06:27,180 --> 00:06:32,760 Anonymous types are class objects derived directly from the system object type. 67 00:06:33,060 --> 00:06:35,640 They can't be cast one of our type. 68 00:06:36,270 --> 00:06:42,030 They override the equals and the cash court methods to support value based equality. 69 00:06:42,480 --> 00:06:48,390 Two anonymous objects with the same values will have the same hash codes, and the equals method will 70 00:06:48,390 --> 00:06:49,990 return to form them. 71 00:06:50,520 --> 00:06:57,130 Please note that the equality operator is not overloaded, so it will return false because there are 72 00:06:57,130 --> 00:07:00,120 reference types and they differ by reference. 73 00:07:00,660 --> 00:07:04,870 Also, anonymous objects support non-destructive mutation. 74 00:07:04,950 --> 00:07:06,120 Well, if there was keyword. 75 00:07:17,480 --> 00:07:24,200 Remember, non-destructive mutation is not changing the original object, but rather creating a new 76 00:07:24,200 --> 00:07:24,630 one. 77 00:07:24,650 --> 00:07:26,060 We've turned to values. 78 00:07:26,930 --> 00:07:31,610 Let's summarize Anonymous types are types without names. 79 00:07:31,910 --> 00:07:39,260 They encapsulate a set of only properties into a single object without having to explicitly define a 80 00:07:39,260 --> 00:07:40,280 type first. 81 00:07:40,550 --> 00:07:47,600 They are most often use defined small, temporary types that we don't intend to use anywhere else. 82 00:07:48,200 --> 00:07:54,410 During the interview, you can be asked Can we modify the value of an anonymous type of property? 83 00:07:55,310 --> 00:07:55,820 No. 84 00:07:56,060 --> 00:08:00,320 All properties of anonymous types are with only one. 85 00:08:00,320 --> 00:08:04,460 Should we owned one, should we not use anonymous types? 86 00:08:04,940 --> 00:08:08,020 The best use case for on animal stopes is one. 87 00:08:08,030 --> 00:08:15,020 The type we want to use is simple and you'll come to some specific context and it will not be used anywhere 88 00:08:15,020 --> 00:08:15,680 else. 89 00:08:16,130 --> 00:08:22,280 Anonymous types are very often used as temporary objects in complex link queries. 90 00:08:22,640 --> 00:08:28,070 If a type is complex or we want to use it, it should not be anonymous. 91 00:08:28,610 --> 00:08:32,930 Also, anonymous types can only provide only properties. 92 00:08:33,320 --> 00:08:40,790 They can't have methods, fields, events, etc. So if we need any of those features, the anonymous 93 00:08:40,820 --> 00:08:42,980 types will not work for us. 94 00:08:43,670 --> 00:08:51,380 Our anonymous types, value or reference types, they are reference types since they are crosses, but 95 00:08:51,380 --> 00:08:54,890 they support value based equality with the equals method. 96 00:08:55,250 --> 00:09:01,430 In other words, two anonymous objects with the same values of properties would be considered equal 97 00:09:01,430 --> 00:09:05,540 by the equals method, even if the references are different. 98 00:09:05,960 --> 00:09:09,350 All right, that's it about anonymous types. 99 00:09:09,620 --> 00:09:12,500 Thanks for watching and see you in the next video.