Reference Types Versus Value Types5:46 with Carling Kirk
We'll use the C# Interactive feature to learn more about the differences between value types and reference types.
In the previous video, we learned a little bit about the difference between value 0:00 types and reference types. 0:04 Let's play around a little with the C# interactive, 0:06 to get a better feel for them. 0:08 In the interactive window, we need to load up our project context so 0:11 we can use the GameResult class. 0:14 We can do this by right-clicking on our project and 0:16 choosing Initialize Interactive with Project. 0:20 It already entered the using declaration for us. 0:26 Let's create a couple of game results to start. 0:31 var result1 = new GameResult. 0:35 And a second one, var result2. 0:41 And we'll assign it to result1. 0:43 So right now, we've got two different variables, 0:49 both pointing to the same object in memory. 0:52 And we'll give result1 a game date value. 0:55 result1.GameDate = DateTime.Now. 0:59 And what do you think the game date value in result2 will be? 1:07 result2.GameDate. 1:11 result1 and result2 are both pointing to the same object. 1:16 So when I change a value on result1, result2 also reflects the change. 1:20 Now let's create a couple of value type variables. 1:24 How about DateTime? 1:27 var date1 equals new DateTime. 1:29 And we'll use a constructor on DateTime and 1:35 pass it a year, a month, and a day. 1:40 And var date2 = date1. 1:45 date1, whoops, date1, 1:50 and date2, both the same value. 1:54 When we assign date1 to date2, it copied the value of date1. 2:00 What do you think will happen if I change the value of date1? 2:04 date1 = date1.AddDays. 2:08 And we'll add 10 days. 2:14 date1, date2. 2:17 Like you saw with the TryParse method, 2:23 we can pass a value type by reference using the out keyword. 2:25 There's another keyword called ref that does the same thing, but 2:29 is typically used in a different circumstance. 2:32 So with the out keyword, we don't need to initialize the variable that gets passed 2:35 in, because it will always be overwritten. 2:39 In fact, we'd get a compiler error if we didn't write anything to the argument that 2:42 was passed in. 2:46 Take a look. 2:47 public bool GetDate(out DateTime date). 2:50 And we won't assign a value, we'll just return true. 3:03 It didn't like that. 3:09 The out parameter date must be assigned to before control leaves the current method. 3:11 I can hit Alt + up arrow to get what we last tried to send to the REPL. 3:16 And in here, we'll assign a value to date. 3:21 Date = DateTime.Now. 3:24 And now it's happy. 3:30 The DateTime parameter we're going to pass it doesn't need to be initialized. 3:32 DateTime rightNow. 3:36 And we'll call GetDate out rightNow. 3:42 And the value of rightNow. 3:49 In contrast, when we use the ref keyword, the value has to be initialized. 3:52 That's usually because it has some kind of significance, and 3:57 its value is used inside the method. 4:00 With value types, you'd really only want to use it if you need to swap values, 4:02 since when you're swapping, 4:07 you'll need both the original values inside the method. 4:08 Let's try it out. 4:11 public bool SwapDates, 4:13 ref keyword, DateTime date1, 4:18 ref DateTime date2. 4:25 Then inside here, we use a temporary 4:30 DateTime tempDate to hold the value of date1. 4:36 We'll assign date1 to the value of date2. 4:44 And then date2 will have the temporary value that temp one had. 4:50 And then we'll return true. 4:56 Let's see how that works. 5:03 rightNow = DateTime.Now. 5:05 Then we'll need another date. 5:12 DateTime later = 5:14 DateTime.Now.AddHours(2). 5:18 And then we'll swap the values with our SwapDates method. 5:27 ref rightNow and ref later. 5:32 Now the value of rightNow is actually what later was. 5:39
You need to sign up for Treehouse in order to download course files.Sign up