I remember that when I took the C # Course in the last semester, the teacher gave us the data type in C # in the first class. We all know that C # is a strong high-level programming language and it is necessary to understand its data type. The instructor gave us a list of many data types, and then asked us which are value types and those are reference types. To be honest, I couldn't answer this question at the time. Maybe I could say that I didn't have much confidence to answer. Although I had been touched by value types and reference types when I was a freshman in C ++, I didn't know the differences because I didn't go into details, or I didn't really understand them, that is why I used it in my own code writing, but I still don't know the specific differences between them.
Now that I am a senior, I am facing a tight interview. I will also look for some interview questions about. net. I learned through some ways that many enterprises put the foundation in an important position during the interview, questions about the value type and reference type that we have mentioned today may often occur in Web interviews such as ASPnet, so I think it is necessary to clarify the following two concepts and their differences.
In the past, I thought that the value type may be understood as the common basic data types (integer, floating point, character, String, struct, and so on ), understanding the reference type as a class or pointer is not comprehensive or even wrong. So what is the value type and the reference type?
In general, the value types of C # include:
Integer: int; long integer: long; floating point: float; character: Char; Boolean: bool; enumeration: Enum; struct: struct
C # has the following reference types:
Class: Class; interface: interface; array: int [], string []; delegate; boxed Value Type
To make it easier for you to remember and understand, I found a graphic display value type and reference type from the Internet, which is clear and easy to understand.
We can see that the value type in C # includes seven data types. Note that not all basic data types are value types, such as string, which is the reference type, we can regard it as a class. Another point is that struct is similar to a class, but it is a value type and a class is a reference type, which is one of their major differences. Then we can accept all the classes and interfaces in the reference type, but we can't forget that arrays are also reference types. The delegate and packing value types can only be remembered because I don't know much about them yet. However, when it comes to delegation, it must be worth mentioning. This is a very important knowledge point in C #. how amazing it is to use the function name as a parameter to pass the call. I am a little excited to think about it. But now I have almost no idea how to use delegation to write code. From this perspective, I have to take a good look at delegation and find a chance to write a blog about "exploring delegation in C.
What are the differences between value types and reference types?
In terms of concept, variables of the value type directly store actual data, while variables of the reference type store the data address, that is, object reference.
From the memory space perspective, value-type data is stored in the memory stack. When the scope ends, the occupied space is released independently, which is highly efficient and does not require address conversion; the reference data is stored in the memory hosting heap. The memory unit only stores the addresses of objects in the heap, and GC controls the collection. address conversion is required to reduce the efficiency, this is one of the reasons why C # needs to define two data types.
The value type is different from the reference type when passing values in function parameters. First, let's look at a piece of code that runs successfully by yourself:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Person { public int Blood = 10; } class Program { public static void Add(int x) { x += 10; Console.WriteLine("值类型当参数被传递并修改之后:" + x); } public static void Add(Person person) { person.Blood += 10; Console.WriteLine("引用类型当参数被传递并修改之后:" + person.Blood); } static void Main(string[] args) { //值类型变量 int i = 10; Console.WriteLine("i的原值:" + i); Add(i); Console.WriteLine("但是i的值并没有因为函数的修改而修改:" + i + ‘\n‘); //引用类型变量 Person person = new Person(); Console.WriteLine("Blood的原值:" + person.Blood); Add(person); Console.WriteLine("但是Blood的值因为函数的修改而修改:" + person.Blood); //值类型和引用类型的区别,就在于当函数参数传递的时候. //值类型是把自己的值复制一份传递给别的函数操作.无论复制的值怎么被改变.其自身的值是不会改变的 //而引用类型是把自己的内存地址传递给别的函数操作.操作的就是引用类型值的本身.所以值被函数改变了. //这就是传值和传址的区别 Console.ReadLine(); } }}
The following results show the differences between the two types of function parameters: (the comments in the above Code explain the reasons)
The article will be written here first. There may be many problems when you write a blog for the first time. If you have any questions, please point out that I will correct them carefully. I wrote this blog to help myself understand some knowledge and notes during the writing process. The research is not so in-depth and may be incomplete. I hope you will forgive me.
Explore the value type and reference type in C #