Home > Net >  In c #, the difference between value types and reference types
In c #, the difference between value types and reference types

Time:11-15

The namespace ConsoleApplication1 {

Class Program {

The static void Main (string [] args) {

ReferenceAndValue. Individual ();//call the individual ReferenceAndValue method

}
}

Public class stamp {//define a class
Public string Name {get; set; }//define a reference type

Public int the Age {get; set; }//define value type
}
Public static class ReferenceAndValue {//define a static class
Public static void individual () {//define a static method

Stamp Stamp_1=new stamp {Name="Premiere", the Age=25}//instantiate
Stamp Stamp_2=new stamp {Name="Again", the Age=47}//instantiate
Int the Age=Stamp_1. Age;//get the value type Age the value of the
Stamp_1. Age=22;//modify the value types of
Stamp the guru=Stamp_2; The values in the//to get Stamp_2
Stamp_2. Name="Again Amend";//modify the reference Name value
Console. WriteLine (" Stamp_1 's age: {0} "Stamp_1. Age);//show the Age of Stamp_1 value
Console. WriteLine (" age "s value: {0}", the age);//show the age the value of the
Console. WriteLine (" Stamp_2 's name: {0} "Stamp_2. Name)//the name of the Stamp_2 value;
Console. WriteLine (" the guru 's name: {0} ", the guru, name);



}
}
}

CodePudding user response:

Value type need not new to the instance, stating that instance, don't not null value type, if want to be null, is the reference object, using the + or type? , in the form of other may still have some differences in performance, especially some pure data value type structure, with a value type may be a little better,

CodePudding user response:

to ask what, code looks not wrong (in addition to the case)

CodePudding user response:

Personal understanding, is wrong, please correct me, didn't also pay more attention to these
Heap program memory is divided into the and stack, heap is called the managed heap, c # is that it will help you recycle

Value type is of simple data, int, float datatime,,,, variable names and values of variables in stack in the
A reference type is variable name and value is a pointer to a variable data in the stack, a variable's value in the pile of
Reference type multiple same variable is used the same variable values in the (heap), is a different variable name, but value is a pointer to a variable data is the same
Go up to a sentence of proof with the lock string ha ha ha, to bring the whole idea of value in the program for ha ha ha string to lock, because the lock is ventilated a variable's value

For reference only reference

CodePudding user response:

A few you can see the

Reference types: a base class for the Objcet
Value type: System. ValueType

Value types: int, long, float, double, byte, short, decimal, struct

Reference types: string, class

Value type variable declarations, whether has the assignment, the compiler allocates memory for its
Reference types when declare a class, an address allocated in the stack, and create a new instance of a class, stack memory address, stockpiling instance

CodePudding user response:

Reference: dynamically modify values
Value: do not modify

CodePudding user response:

Value types are not new

CodePudding user response:

The detailed, the need to first have the address, or the experience of the pointer, have certain heap, stack, the concept of best,
And a reference type:
Is a pointer to a constant [or another description: a record with a the value of the constant address ], was gone

CodePudding user response:

Value types in the stack, a reference type in a heap,
Actually baidu more convenient,

CodePudding user response:

Value types and reference types are not just like the relationship between business and agency business

CodePudding user response:

Value types and reference types are not just like the relationship between business and agency business

CodePudding user response:

Value types and reference types are not just like the relationship between business and agency business

CodePudding user response:

Value types in the stack, a reference type in a heap,
Actually baidu more convenient,
  •  Tags:  
  • C#