In this video Reynald tackles the question of reference types and explain that reference types are objects that store references to the actual data. They’re stored on the heap. Examples: class, object, interface, string, and delegate. Also discussed are value types, which actually hold values. Examples: structs, numeric types, integral types, floating-point types.
- [Instructor] We're going to start with some C-sharp questions. The answers will be concise and short. That's the focus in our objective when we go through this. You can look into the topics separately if you need to spend a little bit more time into it. Please feel free to do so but our goal here is to get a good sense and comfortable with answering questions briefly. One of the most common C-sharp questions that comes up is what's the difference between a reference and value types. A good simple answer to this is reference types are objects that store references to the actual data.
A few examples are classes, interfaces, delegates, objects, and string. That's a good short answer and you provide an example. For value types, well, they actually hold values. Assigning one value type to another literally copies the value. A few examples of those are structs, enums, Booleans, and numeric types. Those are very straightforward answers with some examples. Let's go ahead and take a look at some codes to solidify this.
As you can see here, I have the question and answer for reference types. Right below, we have it for value types. Let's go ahead and add a couple of lines of code here. Specifically, I will be adding an object. We'll call this, let's say its a Book so it's a property which the type is of object. We will set is as a value of "Star Trek," a simple string.
Right below, let' have a simple delegate. Sometimes questions regarding delegates come up also. Essentially, these are function pointers. Let's go ahead and make this a string that we will set in the parameter here for the value. Within our Main method, let's actually use this delegate to write out the string. We'll set default variable as d.
We WIll say that x goes to. We will write out the value of x. Right below, we will invoke our delegate and pass in Book, and convert it ToString. I'll go ahead and save this. I'll make sure this is set to the Startup Project by just right-clicking on the Project and choosing "Set as Startup Project." When we run this, we should just simply see the value of "Star Trek." There's a little error here.
Let's see what happened. Looks like we had an extra parentheses. Now if we'll build, Ctrl + 5 to run, and there's "Star Trek." All I want to do right here is just show that we have our reference types. We have a class like our Program class here that is a reference type. Down here, we didn't use it but there's an interface which is also a reference type. We have our Delegate, an object, that we use here.
For our value types, they are are essentially items such as structs. If we have our book here and we define it as a struct, that is a value type. Enums, we didn't use this but the days here that are defined as enums are value types. Booleans such as on line 51 and numeric types as we indicate in the comments on line 53.
These are just some examples. I wanted to at least have some examples here in code because sometimes when you do whiteboarding, it is a good idea to actually practice writing this out not just on the computer but on a whiteboard. It's good to just get a refresher on the computer screen exactly what these definitions look like. That's it!
- Getting ready for interviews
- Answering questions about generics
- Explaining polymorphism and the four pillars of OOP
- Boxing vs. unboxing in .NET
- Describing page life cycles
- Creating joins vs. inner joins
- Discussing your tech background
- Interviewing for fun
- Collecting notes