Analytical .NET interview questions (01) - type value type and reference

  Common interview questions:

1. The value types and reference types difference?

2. The difference between the structure and the like?

3. delegate is a reference type or a value type? enum, int [] and string it?

4. The difference between stack and heap?

Data will be allocated on the heap (stack) 5. Under what circumstances? They do have a difference in performance?

6. "structure" may be allocated on the heap objects it? Under what circumstances would happen, what needs to pay attention to it?

7. understand the argument is passed by value? And pass-by-reference?

8. outand refthe difference between the same point?

9. C # which supports several predefined value types? C # which supports predefined reference types?

10. There are several ways determination value and reference type?

11. Talk about value types and reference types of life cycle?

12. If the definition of the structure reference types, object in memory is how to store? For example, the following structure of the object class User class is stored on the stack or heap?

public struct MyStruct 
{ 
    public int Index; 
    public User User; 

  Understanding value types and reference types

Remain the same, as long as the clear value types and reference types in principle, subject to all of the above have been solved.

smile basic concept

CLR supports two types: reference types and value types . This is the basis and key .NET language, they, create an instance of the type definitions, parameters are passed to the memory allocation are different. Although it seems simple, but really understand its meaning seems not many people have.

Picture references

The following figure shows clearly the .NET type of classification, the value of the main types are simple, basic data type, a reference type is mainly used for a richer, complicated, complex data types.

laughing out loud Memory Architecture

Its memory allocation is the difference difference between value types and reference types most causes of, before we must first understand the CLR memory in two important concepts:

Stack Stack : thread stack, operating system management, storage of value type reference type variable (the address is a reference to an object in the managed heap) . Stack is thread-based, which means that a thread will contain a thread stack, thread stack value type will be cleaned at the end of the subject scope, efficiency is very high.

GC Heap managed heap : After the initialization process is divided in the process address space memory space, the process of storing objects in the .NET runtime, all reference types are allocated on the managed heap , the managed heap allocated objects are managed by the GC and release. Managed heap is process-based, of course, there are other managed heap more complex internal structure, are interested can understand.

Understood in conjunction with the FIG., The value of the variable a and 3 are stored on the stack top. The variable b is stored on the stack, which points to a string value "123" address managed heap objects (character string is a reference type, the string is stored in the managed heap objects above. Reference string is a special type, later in the article will specifically Discussion) "

image

Value types have been stored on the stack on top of it? All reference types are stored in the managed heap on top of it?

1. The individual values ​​of a variable type, such as the type of the local variable values ​​are stored in the stack above;

2. When the value of type is a custom class field attribute, it is stored with reference to the type of managed heap, when she is part of a reference type;

4. All reference types are certainly stored in the managed heap.

5. In another case, the subject with the above 12, define a reference type field (Type value) in the structure, the structure is stored on the stack, which reference variable field stores only the memory address, pointing to a reference example of the heap.

Wink Passing objects

Assigning a value type variable to another variable (or passed as a parameter), performs a replication value. The reference type variable to another variable of reference type, it copies the value of the memory address referenced object, and therefore will be assigned after a plurality of variable reference point to the same object instance. It is important to understand the following code to test to test:

int v1 = 0;
            int v2 = v1;
            v2 = 100;
            Console.WriteLine("v1=" + v1); //输出:v1=0
            Console.WriteLine("v2=" + v2); //输出:v2=100

            User u1=new User();
            u1.Age = 0;
            User u2 = u1;
            u2.Age = 100;
            Console.WriteLine("u1.Age=" + u1.Age); //输出:u1.Age=100
            Console.WriteLine("u2.Age=" + u2.Age); //输出:u2.Age=100,因为u1/u2指向同一个对象

当把对象作为参数传递的时候,效果同上面一样,他们都称为按值传递,但因为值类型和引用类型的区别,导致其产生的效果也不同。

参数-按值传递:

private void DoTest(int a)
        {
            a *= 2;
        }

        private void DoUserTest(User user)
        {
            user.Age *= 2;
        }

        [NUnit.Framework.Test]
        public void DoParaTest()
        {
            int a = 10;
            DoTest(a);
            Console.WriteLine("a=" + a); //输出:a=10
            User user = new User();
            user.Age = 10;
            DoUserTest(user);
            Console.WriteLine("user.Age=" + user.Age); //输出:user.Age=20
        }

上面的代码示例,两个方法的参数,都是按值传递

  • 对于值类型(int a) :传递的是变量a的值拷贝副本,因此原本的a值并没有改变。
  • 对于引用类型(User user) :传递的是变量user的引用地址(User对象实例的内存地址)拷贝副本,因此他们操作都是同一个User对象实例。

参数-按引用传递:

按引用传递的两个主要关键字:outref不管值类型还是引用类型,按引用传递的效果是一样的,都不传递值副本,而是引用的引用(类似c++的指针的指针)。outref告诉编译器方法传递额是参数地址,而不是参数本身,理解这一点很重要。

代码简单测试一下,如果换成out效果是相同的

private void DoTest( ref int a)
        {
            a *= 2;
        }

        private void DoUserTest(ref User user)
        {
            user.Age *= 2;
        }

        [NUnit.Framework.Test]
        public void DoParaTest()
        {
            int a = 10;
            DoTest(ref a);
            Console.WriteLine("a=" + a); //输出:a=20 ,a的值改变了
            User user = new User();
            user.Age = 10;
            DoUserTest(ref user);
            Console.WriteLine("user.Age=" + user.Age); //输出:user.Age=20
        }

outref的主要异同

  • outref都指示编译器传递参数地址,在行为上是相同的;
  • 他们的使用机制稍有不同,ref要求参数在使用之前要显式初始化,out要在方法内部初始化;
  • outref不可以重载,就是不能定义Method(ref int a)和Method(out int a)这样的重载,从编译角度看,二者的实质是相同的,只是使用时有区别;

Tongue smile 常见问题

  题目答案解析:

1. 值类型和引用类型的区别?

值类型包括简单类型、结构体类型和枚举类型,引用类型包括自定义类、数组、接口、委托等。

  • 1、赋值方式:将一个值类型变量赋给另一个值类型变量时,将复制包含的值。这与引用类型变量的赋值不同,引用类型变量的赋值只复制对象的引用(即内存地址,类似C++中的指针),而不复制对象本身。
  • 2、继承:值类型不可能派生出新的类型,所有的值类型均隐式派生自 System.ValueType。但与引用类型相同的是,结构也可以实现接口。
  • 3、null:与引用类型不同,值类型不可能包含 null 值。然而,可空类型允许将 null 赋给值类型(他其实只是一种语法形式,在clr底层做了特殊处理)。
  • 4、每种值类型均有一个隐式的默认构造函数来初始化该类型的默认值,值类型初始会默认为0,引用类型默认为null。
  • 5、值类型存储在栈中,引用类型存储在托管堆中。

2. 结构和类的区别?

结构体是值类型,类是引用类型,主要区别如题1。其他的区别:

  • 结构不支持无惨构造函数,不支持析构函数,并且不能有protected修饰;
  • 结构常用于数据存储,类class多用于行为;
  • class需要用new关键字实例化对象,struct可以不适用new关键字;
  • class可以为抽象类,struct不支持抽象;

3. delegate是引用类型还是值类型?enum、int[]和string呢?

enum枚举是值类型,其他都是引用类型。

4. 堆和栈的区别?

线程堆栈:简称栈 Stack
托管堆: 简称堆 Heap
  • 值类型大多分配在栈上,引用类型都分配在堆上;
  • 栈由操作系统管理,栈上的变量在其作用域完成后就被释放,效率较高,但空间有限。堆受CLR的GC控制;
  • 栈是基于线程的,每个线程都有自己的线程栈,初始大小为1M。堆是基于进程的,一个进程分配一个堆,堆的大小由GC根据运行情况动态控制;

6.“结构”对象可能分配在堆上吗?什么情况下会发生,有什么需要注意的吗?

结构是值类型,有两种情况会分配在对上面:

  • 结构作为class的一个字段或属性,会随class一起分配在堆上面;
  • 装箱后会在堆中存储,尽量避免值类型的装箱,值类型的拆箱和装箱都有性能损失,下一篇会重点关注;

7. 理解参数按值传递?以及按引用传递?

  • 按值传递:对于值类型传递的它的值拷贝副本,而引用类型传递的是引用变量的内存地址,他们还是指向的同一个对象。
  • 按引用传递:通过关键字out和ref传递参数的内存地址,值类型和引用类型的效果是相同的。

8. outref的区别与相同点?

  • outref都指示编译器传递参数地址,在行为上是相同的;
  • 他们的使用机制稍有不同,ref要求参数在使用之前要显式初始化,out要在方法内部初始化;
  • outref不可以重载,就是不能定义Method(ref int a)和Method(out int a)这样的重载,从编译角度看,二者的实质是相同的,只是使用时有区别;

9. C#支持哪几个预定义的值类型?C#支持哪些预定义的引用类型?

值类型:整数、浮点数、字符、bool和decimal

Reference types: Object, String

10. There are several ways determination value and reference type?

In simple terms, inherited from System.ValueType is a value type, on the contrary is a reference type.

11. Talk about value types and reference types of life cycle?

Value Type released after the end of the scope.

Reference types recovered from the GC garbage payback period. The answer may be too simple, more detailed answer in a later article will talk.

12. If the definition of the structure reference types, object in memory is how to store? For example, the following structure of the object class User class is stored on the stack or heap?

public struct MyStruct 
{ 
    public int Index; 
    public User User; 
}

MyStruct stored on the stack, which is stored in the field User examples heap, MyStruct.User field stores the memory address pointing to the User object.

Guess you like

Origin www.cnblogs.com/ljdong7/p/12014527.html