0 1 00:00:06,990 --> 00:00:07,490 Hello. 1 2 00:00:08,250 --> 00:00:14,760 The next interview question we're going to discuss is "What is the difference between "is" and "as" keywords?" 2 3 00:00:15,240 --> 00:00:19,830 The "is" keyword is used to check if an object is of a given type. 3 4 00:00:20,130 --> 00:00:22,620 It returns a Boolean as a result. 4 5 00:00:27,850 --> 00:00:35,200 In this case, the result will be true because the text variable is a string. We can use the "is" keyword 5 6 00:00:35,260 --> 00:00:38,140 with value types and reference types as well. 6 7 00:00:42,050 --> 00:00:49,070 In this case, the result will be false because the text variable is not an integer. The "is" keyword 7 8 00:00:49,070 --> 00:00:54,650 is most often used to ensure that a type can be safely cast to some other type. 8 9 00:00:55,100 --> 00:00:59,330 We can also have some business logic driven by the type of some variable. 9 10 00:00:59,570 --> 00:01:03,080 In the case of type A, we want to execute different logic 10 11 00:01:03,170 --> 00:01:09,680 then in the case of type B. The "as" keyword is used to cast a variable to a given type. 11 12 00:01:14,410 --> 00:01:17,290 Here I am casting an object to a string. 12 13 00:01:17,650 --> 00:01:21,640 The cast will be successful and the result will be the string "Hello!". 13 14 00:01:22,000 --> 00:01:25,870 If the cast would not be successful, the result would be null. 14 15 00:01:33,640 --> 00:01:36,760 As you can see, the first variable has a value. 15 16 00:01:36,970 --> 00:01:38,780 But the second is null. 16 17 00:01:39,520 --> 00:01:45,480 The fact that in the case of invalid casting, the result will be null is the crucial difference between 17 18 00:01:45,500 --> 00:01:49,480 casting with "as" keyword and regular casting with parentheses. 18 19 00:01:49,840 --> 00:01:53,290 Regular casting throws an exception when the cast fails. 19 20 00:01:53,620 --> 00:01:54,970 Let's see this in code. 20 21 00:02:01,310 --> 00:02:09,650 This cast simply returned a null, but this caused InvalidCastException. Because casting with "as" can 21 22 00:02:09,650 --> 00:02:10,560 return null 22 23 00:02:10,760 --> 00:02:16,850 it can only be used with nullable types, so all reference types plus nullable value types. 23 24 00:02:17,150 --> 00:02:20,240 It's not valid with the non-nullable value types. 24 25 00:02:25,550 --> 00:02:32,060 This doesn't work because when casting fails (and it will in this case), we will try to assign null to 25 26 00:02:32,060 --> 00:02:37,670 an integer which is not legal in C# because integer is a non-nullable value type. 26 27 00:02:38,420 --> 00:02:41,150 Let's summarize. The "is" keyword checks 27 28 00:02:41,150 --> 00:02:48,920 if the object is of a given type. It returns a boolean result. The "as" keyword casts an object 28 29 00:02:48,920 --> 00:02:50,000 to a given type. 29 30 00:02:50,240 --> 00:02:55,310 It can only be used for casting to reference types or nullable value types. 30 31 00:02:55,910 --> 00:03:02,390 During the interview, you might be asked "What is the difference between regular cast and casting with 31 32 00:03:02,390 --> 00:03:02,810 "as" keyword?" 32 33 00:03:03,710 --> 00:03:08,000 Well, if casting with "as" fails, the result will simply be null. 33 34 00:03:08,390 --> 00:03:08,750 When 34 35 00:03:08,750 --> 00:03:12,800 regular casting fails an InvalidCastException will be thrown. 35 36 00:03:13,490 --> 00:03:20,210 Another question you can hear is "Why can we only use the "as" keyword to cast objects to nullable types?" 36 37 00:03:20,810 --> 00:03:27,980 Because if casting with "as" fails, null will be returned. Null can only be assigned to nullable types. 37 38 00:03:28,790 --> 00:03:32,300 All right, that's it about the "is" and "as" keywords. 38 39 00:03:32,810 --> 00:03:35,540 Thanks for watching, and I'll see you in the next lecture.