, ValueTypes
ValueType . , , - .
int = 10;
int j = i;
, , j. , -. , , valuetype valuetype, .
.
o = 10;
p = o;
o - ReferenceType o , 10 ( , ). p . , .
1.
2. ( ), "".
, .
# . , valueType , valuetype,
int i = 10;
SomeMethod(i);
Console.WriteLine(i);
static void SomeMethod(int value)
{
value = 20;
}
SomeMethod, . , i. 10:
;
class Program
{
static void Main(string[] args)
{
Customer c = new Customer() { Name = "Mike" };
SomeMethod(c);
Console.WriteLine(c.Name);
}
static void SomeMethod(Customer customer)
{
customer.Name = "John";
}
}
class Customer
{
public string Name { get; set; }
}
c - . # . "" . c, . , ( , ), . "", "".
, ( "" ). .
class Program
{
static void Main(string[] args)
{
Customer c = new Customer() { Name = "Mike" };
SomeMethod(c);
Console.WriteLine(c.Name);
}
static void SomeMethod(Customer customer)
{
customer = new Customer();
customer.Name = "John";
}
}
class Customer
{
public string Name { get; set; }
}
, , "". , , - "", .
, (), . , , . ?
, . , , SomeMethod , ? , , . , c ( , , ). , .
class Program
{
static void Main(string[] args)
{
Customer c = new Customer() { Name = "Mike" };
SomeMethod(ref c);
Console.WriteLine(c.Name);
}
static void SomeMethod(ref Customer customer)
{
customer = new Customer();
customer.Name = "John";
}
}
class Customer
{
public string Name { get; set; }
}