1 00:00:07,230 --> 00:00:07,670 Hello. 2 00:00:08,190 --> 00:00:16,110 In this lecture, we're going to talk about Question 31, what our records and records structs when 3 00:00:16,110 --> 00:00:23,190 programming, we often need to define simple data structures that don't really hold any business logic. 4 00:00:23,460 --> 00:00:25,170 They simply stole data. 5 00:00:25,650 --> 00:00:27,450 Let's define a point class. 6 00:00:27,990 --> 00:00:31,800 This class only holds two integers X and Y. 7 00:00:32,250 --> 00:00:35,370 I want the object of this class to be immutable. 8 00:00:35,490 --> 00:00:38,250 So one cert they will not be updated. 9 00:00:38,520 --> 00:00:42,840 That's why I only added letters to the X and Y properties. 10 00:00:43,140 --> 00:00:45,150 Setters are not available. 11 00:00:45,600 --> 00:00:50,250 First of all, I would like the objects of this class to be nicely printed. 12 00:00:50,670 --> 00:00:53,370 Let's see how this object will be printed now. 13 00:00:54,930 --> 00:01:02,040 Well, currently the full name of the type printed, but I would rather see something like X is done 14 00:01:02,040 --> 00:01:03,660 and Y is twenty. 15 00:01:04,020 --> 00:01:07,250 That's why we should overwrite the two string method. 16 00:01:15,530 --> 00:01:16,160 Perfect. 17 00:01:16,610 --> 00:01:17,810 That looks much better. 18 00:01:19,070 --> 00:01:26,810 Next, I would like to use objects of the pond grass, a squeeze in a dictionary currently, since point 19 00:01:26,810 --> 00:01:34,790 is across the guitar called method basis on reference and so does the equals method, I should overwrite 20 00:01:34,790 --> 00:01:42,740 those methods if I wanted the dictionary to recognize objects of the point grass by value, not by reference. 21 00:02:00,580 --> 00:02:07,870 No, I must also provide that equals method that accepts a point class object, not an instance of system 22 00:02:07,870 --> 00:02:08,740 object type. 23 00:02:09,100 --> 00:02:15,280 This means my point class shall implement the équitable of both class interface. 24 00:02:27,170 --> 00:02:27,830 All right. 25 00:02:28,100 --> 00:02:29,840 Let's see the whole class. 26 00:02:31,610 --> 00:02:39,080 Well, it will work, but it's a lot of code and all of it only to implement a simple data structure 27 00:02:39,080 --> 00:02:48,490 that brings itself nicely is compared by value, provides custom guitar code and equals method implementations 28 00:02:48,710 --> 00:02:53,120 so it can safely be used in collections at some point. 29 00:02:53,270 --> 00:02:59,060 The creators of C-sharp realized that this is a common issue as a solution. 30 00:02:59,300 --> 00:03:04,670 They introduced records before I explain exactly what records are. 31 00:03:04,940 --> 00:03:11,630 Let me show you how exactly the same behavior as we defined in the point class type can be achieved 32 00:03:11,690 --> 00:03:12,680 with records. 33 00:03:19,930 --> 00:03:21,190 And that's it. 34 00:03:21,640 --> 00:03:23,230 Only one line of code. 35 00:03:23,410 --> 00:03:27,880 And it does exactly the same thing as 31 lines of code. 36 00:03:28,180 --> 00:03:30,310 We need that to define the point. 37 00:03:30,310 --> 00:03:35,740 Class records are new types, joining classes and structs. 38 00:03:36,250 --> 00:03:38,890 They are available starting with the shop. 39 00:03:38,890 --> 00:03:39,370 Nine. 40 00:03:39,910 --> 00:03:43,810 Let's list the most important information about records. 41 00:03:44,530 --> 00:03:52,720 Records are reference types, but they based on value type equality, which means two records with identical 42 00:03:52,720 --> 00:04:00,010 values of properties will be considered equal even if they differ by reference like classes. 43 00:04:00,220 --> 00:04:01,840 They support inheritance. 44 00:04:02,500 --> 00:04:10,360 The compiler generates the following methods for their records and overwrite of the equals method accepting 45 00:04:10,360 --> 00:04:16,990 an object a virtual equals method accepting another instance of this record type. 46 00:04:17,380 --> 00:04:24,670 This method comes from the équitable interface, which records implement an override for the guitar. 47 00:04:24,880 --> 00:04:28,750 Automated overloads loads of equality operators. 48 00:04:29,200 --> 00:04:35,650 An override of the two string method, which brings the names of the properties with their values. 49 00:04:36,550 --> 00:04:39,790 The record we defined here is even more special. 50 00:04:40,330 --> 00:04:45,220 It's so-called positional record, so a record that doesn't even have a body. 51 00:04:45,610 --> 00:04:50,650 Later in this video, we learn how to define non positional records. 52 00:04:51,130 --> 00:04:58,390 For now, let's just note that for positional records, the compiler also generates a primary constructor 53 00:04:58,600 --> 00:05:05,260 whose parameters match the positional parameters of the record declaration public properties for each 54 00:05:05,260 --> 00:05:07,180 parameter of this constructor. 55 00:05:07,780 --> 00:05:14,650 Those properties are read only, but they are not for record struct which will learn about in a minute. 56 00:05:15,040 --> 00:05:19,330 Also, I deconstruct method, which allows using deconstruction. 57 00:05:19,930 --> 00:05:24,280 All right, now, let's see a regular positional record. 58 00:05:26,010 --> 00:05:32,270 As you can see, we need to write a little more code, like explicitly defining the properties and the 59 00:05:32,280 --> 00:05:37,650 constructor, but as a reward, we can add methods or make the properties readable. 60 00:05:37,950 --> 00:05:44,820 Remember that methods like hash code equals or to string are still generated by the compiler, and we 61 00:05:44,820 --> 00:05:48,980 don't need to worry about them, as we learned in the last lecture. 62 00:05:49,020 --> 00:05:52,200 The immutability of types is a desired trait. 63 00:05:52,530 --> 00:05:58,410 Records are perfect for representing immutable types to make things even easier. 64 00:05:58,590 --> 00:06:03,330 They provide non-destructive annotation implemented with the wish keyword. 65 00:06:03,750 --> 00:06:06,750 This may sound cryptic, so let's see an example. 66 00:06:07,140 --> 00:06:13,620 Let's say I have some bond and I want to update its Y property, but in a non-destructive way. 67 00:06:26,970 --> 00:06:33,750 With the work you worked, I created a new point equal to the old one, but with one set to the new 68 00:06:33,750 --> 00:06:38,340 value, the old bond is immutable, so it cannot be changed. 69 00:06:38,640 --> 00:06:41,160 I turn as many properties as I want. 70 00:06:41,430 --> 00:06:47,370 There were few words starting with C-sharp, then records structs were introduced. 71 00:06:47,700 --> 00:06:49,230 They are similar to records. 72 00:06:49,390 --> 00:06:51,000 Well, some differences. 73 00:06:51,300 --> 00:06:55,440 First of all, they are value tags, positional records. 74 00:06:55,440 --> 00:07:00,660 Structs are read right by default, which means their properties are immutable. 75 00:07:01,080 --> 00:07:05,730 Record structs can be declared as read-only, making them immutable. 76 00:07:20,030 --> 00:07:27,460 So I want to be able to modify the instances of this struct because it is with only but for disrupts, 77 00:07:27,650 --> 00:07:33,680 the properties are immutable when deciding whether to use records or reconstruct. 78 00:07:33,800 --> 00:07:40,130 We should take the same things into consideration as when deciding whether to use classis or structs. 79 00:07:40,550 --> 00:07:47,240 If the type is small and simple and you want value Typekit behavior like putting parameters by value 80 00:07:47,240 --> 00:07:49,700 or assignment that creates a copy. 81 00:07:49,790 --> 00:07:55,520 You should go for the record struct records and record struct support the. 82 00:07:56,690 --> 00:08:00,380 We learned about it in the What is the construction lecture? 83 00:08:03,890 --> 00:08:11,300 Let's summarize records and records tracts are new types introduced in Europe nine and 10. 84 00:08:11,690 --> 00:08:15,680 They are mostly used to define simple types representing data. 85 00:08:16,100 --> 00:08:18,410 They support value based equality. 86 00:08:18,830 --> 00:08:22,010 They make it very easy to create immutable types. 87 00:08:22,620 --> 00:08:24,260 Questions about records. 88 00:08:24,470 --> 00:08:30,530 Are recently quite common during the interviews because the interviewers often wanted to check if you 89 00:08:30,530 --> 00:08:33,260 are up to date with the new versions of C-sharp. 90 00:08:33,530 --> 00:08:38,420 So, for example, you can hear a question What is the purpose of the whiskey worth? 91 00:08:38,900 --> 00:08:46,610 The whiskey world is used to create a copy of our record object, with some properties set to new values. 92 00:08:46,910 --> 00:08:51,980 In other words, is used to perform a non-destructive imitation of records. 93 00:08:52,460 --> 00:08:54,500 What are positional records? 94 00:08:54,920 --> 00:08:56,150 Positional records? 95 00:08:56,150 --> 00:08:58,190 Our records with nobodys. 96 00:08:58,550 --> 00:09:04,160 The compiler generates properties constructor and deconstruct method for them. 97 00:09:04,560 --> 00:09:11,270 There are a shorter way of defining records, but we can't add custom methods or about properties to 98 00:09:11,270 --> 00:09:12,590 our position on record. 99 00:09:13,280 --> 00:09:14,660 All right, that's it. 100 00:09:14,660 --> 00:09:20,750 In this lecture, I hope you'll find it useful and that you start using records in your code. 101 00:09:21,230 --> 00:09:24,080 Thanks for watching and see you in the next video.