1 00:00:00,570 --> 00:00:02,550 In object-oriented programming, 2 00:00:02,550 --> 00:00:07,630 the class that uses another class is called the client of the class. 3 00:00:07,630 --> 00:00:12,480 Don't confuse the term client here with client server or business client. 4 00:00:12,480 --> 00:00:16,490 A client is simply a class that uses another class in some way. 5 00:00:17,520 --> 00:00:20,750 We often want to make it so that clients of a class can 6 00:00:20,750 --> 00:00:25,170 access the items inside of a collection that's stored in the class. 7 00:00:25,170 --> 00:00:29,650 Let's make a property in the SchoolRoll class that exposes the students list. 8 00:00:30,840 --> 00:00:35,170 Let's add a property named Students here to return the students list. 9 00:00:35,170 --> 00:00:41,950 So say public List of student, call it students and 10 00:00:41,950 --> 00:00:48,742 have the getter return the private students list. 11 00:00:51,927 --> 00:00:54,670 Students is a read-only property. 12 00:00:54,670 --> 00:00:57,480 It doesn't have a setter, only a getter. 13 00:00:57,480 --> 00:01:00,000 All this means is that clients of this class can't 14 00:01:00,000 --> 00:01:02,740 overwrite the private students field up here. 15 00:01:02,740 --> 00:01:05,960 However, they can still alter the students list. 16 00:01:05,960 --> 00:01:10,295 Back here in main, let's add this list of students to the SchoolRoll. 17 00:01:11,410 --> 00:01:13,650 I've cleared out everything in main except for 18 00:01:13,650 --> 00:01:16,730 where we're creating the student list and where we're printing it out. 19 00:01:17,980 --> 00:01:21,060 We'll create an instance of SchoolRoll and 20 00:01:21,060 --> 00:01:23,893 then add students to it using the addStudents method. 21 00:01:23,893 --> 00:01:30,987 So say SchoolRoll schoolRoll 22 00:01:30,987 --> 00:01:38,889 = new SchoolRoll And 23 00:01:38,889 --> 00:01:44,284 then schoolRoll.AddStudents. 24 00:01:44,284 --> 00:01:46,580 And then we'll pass in the students list. 25 00:01:49,170 --> 00:01:51,345 Down here we'll want to change this to 26 00:01:51,345 --> 00:01:59,950 SchoolRoll.Students, so 27 00:01:59,950 --> 00:02:02,088 that we're printing out all of the students in the school. 28 00:02:02,088 --> 00:02:06,440 Having this Students property is great 29 00:02:06,440 --> 00:02:08,840 because it gives us access to the students. 30 00:02:08,840 --> 00:02:13,550 But because we've exposed this students list using this property in the SchoolRoll 31 00:02:13,550 --> 00:02:20,350 class, we can do what ever we want to the students list such as remove the students. 32 00:02:20,350 --> 00:02:29,815 So we can do something like schoolRoll.Students.RemoveAt(0) or 33 00:02:29,815 --> 00:02:32,503 we could sort them. 34 00:02:39,478 --> 00:02:43,420 This could cause problems if the student roll class was reliant on the students 35 00:02:43,420 --> 00:02:45,160 being in a certain order already. 36 00:02:46,270 --> 00:02:50,790 Or we could add more students and completely bypass the AddStudent method. 37 00:02:50,790 --> 00:03:00,317 So we could do something like schoolRoll.Students.AddRange(students):. 38 00:03:00,317 --> 00:03:04,637 This is dangerous because there might have been some reason why we wanted clients of 39 00:03:04,637 --> 00:03:05,880 our SchoolRoll class. 40 00:03:05,880 --> 00:03:09,710 To use the AddStudents method instead of adding them directly to the list 41 00:03:09,710 --> 00:03:10,350 like this. 42 00:03:12,360 --> 00:03:16,467 Perhaps the AddStudents method had checked that duplicate students weren't being 43 00:03:16,467 --> 00:03:18,380 added to the rolls. 44 00:03:18,380 --> 00:03:22,190 By allowing this type of write access to the student list directly 45 00:03:22,190 --> 00:03:23,860 via the student property. 46 00:03:23,860 --> 00:03:29,140 We've created an opportunity for clients of the SchoolRoll class to introduce bugs. 47 00:03:29,140 --> 00:03:30,490 So what do we do? 48 00:03:30,490 --> 00:03:33,890 Once again, interfaces can save the day here as well. 49 00:03:33,890 --> 00:03:37,781 If we only wanted clients of our SchoolRoll class to be able to loop 50 00:03:37,781 --> 00:03:42,906 through the students, we could change the type of our property here to IEnumerable. 51 00:03:48,971 --> 00:03:53,898 We can return the student list as an IEnumerable because the list class is 52 00:03:53,898 --> 00:03:54,960 IEnumerable. 53 00:03:56,177 --> 00:03:59,365 Since IEnumerable doesn't expose any methods that can 54 00:03:59,365 --> 00:04:01,420 alter the list in any way. 55 00:04:01,420 --> 00:04:04,420 This essentially makes the list read only from the point of view of 56 00:04:04,420 --> 00:04:06,050 the client classes. 57 00:04:06,050 --> 00:04:09,390 Take a look at the other interfaces that the List class implements. 58 00:04:10,540 --> 00:04:15,255 You'll notice a number of interfaces that start with IReadOnly. 59 00:04:15,255 --> 00:04:19,640 IReadOnly collection allows clients to see how many students are in the list using 60 00:04:19,640 --> 00:04:25,430 the count property and IReadOnly list allows clients to index into the list. 61 00:04:25,430 --> 00:04:28,320 We could have used either of these collection types as well. 62 00:04:28,320 --> 00:04:30,930 The purpose of read-only collection interfaces 63 00:04:30,930 --> 00:04:34,700 is not to provide some level of security against malicious coders, 64 00:04:34,700 --> 00:04:37,750 the underlying collection here is still a list. 65 00:04:39,030 --> 00:04:42,150 If we really wanted access to the list again, 66 00:04:42,150 --> 00:04:47,410 we just need to cast the object returned by the student's property back to a list. 67 00:04:47,410 --> 00:04:50,800 This is just another form of type encapsulation. 68 00:04:50,800 --> 00:04:55,200 By exposing the list this way, we are making it clear to other developers 69 00:04:55,200 --> 00:04:58,270 how we expect the SchoolRoll class to be used. 70 00:04:58,270 --> 00:05:02,775 You might say that this way our code is self-documenting. 71 00:05:02,775 --> 00:05:06,095 We should always try to write clear self-documenting code 72 00:05:06,095 --> 00:05:10,065 in order to help other coders avoid accidentally misusing it. 73 00:05:10,065 --> 00:05:12,475 This can greatly reduce the chances for bugs.