C# - 引用类型仍然被裁判需要通行证? [英] C# - Reference type still needs pass by ref?
问题描述
考虑以下code(为简单起见,我没有遵循任何C#编码规则)。
公共类教授
{
公共字符串_Name; 公共教授(){} 公共教授(字符串名称)
{
_name =名称;
} 公共无效显示()
{
Console.WriteLine(NAME = {0},_名称);
}
}公共类实例
{
静态INT主要(字串[] args)
{
大卫教授=新教授(大卫); Console.WriteLine(\\ N之前调用方法ProfessorDetails()..);
david.Display();
ProfessorDetails(大卫)
Console.WriteLine(\\ n在调用方法ProfessorDetails()..);
大卫。显示();
} 静态无效ProfessorDetails(教授P)
{
//在这里更名反映
p._Name =花; //为什么来电无法看到这个任务
P =新教授(乔恩);
}
}
正如所预期的输出结果是:
调用方法ProfessorDetails()之前...
名称=大卫
调用方法ProfessorDetails()后...
名称=花
块引用>呼叫
P =新教授(乔恩);在
ProfessorDetails(教授P)
是无效的,即使它是引用类型。我为什么还需要使用REF
关键字来获得期望的结果?
解决方案
一切都在C#中值传递
当你使用它告诉编译器将原来的引用,而不是拷贝'裁判'的关键字,这样你就可以修改什么引用指向函数中。但是,这方面的需要通常是罕见的,是最常用的,当你需要从方法返回多个值。
一个例子:
类Foo
{
INT ID {搞定;组; } 公共美孚(INT ID)
{
ID = ID;
}
}无效的主要()
{
美孚F =新的Foo(1);
Console.WriteLine(f.ID); //输出1
ChangeId(F);
Console.WriteLine(f.ID); //输出5
CHANGEREF(F);
Console.WriteLine(f.ID); //仍然打印5,不仅改变了什么副本是指向
}静态无效ChangeId(美孚F)
{
f.ID = 5;
}静态无效CHANGEREF(美孚F)
{
F =新的Foo(10);
}
Consider the following code (for simplicity, I did not follow any C# coding rules).
public class Professor
{
public string _Name;
public Professor(){}
public Professor(string name)
{
_Name=name;
}
public void Display()
{
Console.WriteLine("Name={0}",_Name);
}
}
public class Example
{
static int Main(string[] args)
{
Professor david = new Professor("David");
Console.WriteLine("\nBefore calling the method ProfessorDetails().. ");
david.Display();
ProfessorDetails(david);
Console.WriteLine("\nAfter calling the method ProfessorDetails()..");
david. Display();
}
static void ProfessorDetails(Professor p)
{
//change in the name here is reflected
p._Name="Flower";
//Why Caller unable to see this assignment
p=new Professor("Jon");
}
}
As expected the output is :
Before calling the method ProfessorDetails()...
Name =David
After calling the method ProfessorDetails()...
Name =Flower
The call p=new Professor("Jon");
in ProfessorDetails(Professor p)
is not effective, even though it is reference type. Why should i still need to use the ref
keyword to get the desired result?
Everything is passed by value in C#. However, when you pass a reference type, the reference itself is being passed by value, i.e., a copy of the original reference is passed. So, you can change the state of object that the reference copy points to, but if you assign a new value to the reference you are only changing what the copy points to, not the original reference.
When you use the 'ref' keyword it tells the compiler to pass the original reference, not a copy, so you can modify what the reference points to inside of the function. However, the need for this is usually rare and is most often used when you need to return multiple values from a method.
An example:
class Foo
{
int ID { get; set; }
public Foo( int id )
{
ID = id;
}
}
void Main( )
{
Foo f = new Foo( 1 );
Console.WriteLine( f.ID ); // prints "1"
ChangeId( f );
Console.WriteLine( f.ID ); // prints "5"
ChangeRef( f );
Console.WriteLine( f.ID ); // still prints "5", only changed what the copy was pointing to
}
static void ChangeId( Foo f )
{
f.ID = 5;
}
static void ChangeRef( Foo f )
{
f = new Foo( 10 );
}
这篇关于C# - 引用类型仍然被裁判需要通行证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!