分配对变量的引用 [英] Assigning a reference to a variable

查看:75
本文介绍了分配对变量的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个结构,例如:


struct Settings {

public string str;

public double doub1 ,doub2;

public int i1;

}


如何将i1的引用分配给变量?我可以通过将它作为ref int

传递给函数来轻松完成此操作,但有没有办法将它传递给函数?

解决方案

11月26日下午4:44,Jon < .wrote:


如果我有结构,例如:


struct Settings {

public string str;

public double doub1,doub2;

public int i1;


}


如何将i1的引用分配给变量?我可以通过将它作为ref int

传递给函数来轻松完成此操作,但有没有办法在不将其传递给函数的情况下执行此操作?



编号如果你想让一个类型表现得像一个引用类型,你应该

使它成为一个引用类型。


Jon


Jon写道:


如果我有结构,例如:


struct Settings {

public string str;

public double doub1,doub2;

public int i1;

}


如何将i1的引用分配给变量?我可以通过将它作为ref int

传递给函数来轻松完成此操作,但有没有办法在不将其传递给函数的情况下执行此操作?



你可以使用不安全的代码来做,但我不建议这样做。如果您使用不安全的代码,则语法与C ++非常相似。


设置集=新设置();

sets.i1 = 5;

int * x =& sets.i1;

* x = 6;


控制台.WriteLine(sets.i1.ToString());

-

Tom Porterfield


谢谢你的支持回复Jon + Tom。


我来自C背景,但我真的不想使用不安全的代码。


如果你想让一个类型表现得像一个引用类型,你应该把它作为一个引用类型来启动

with。如何将整数作为引用类型?


Jon

" Jon" < .wrote in message news:Oq ************* @ TK2MSFTNGP06.phx.gbl ...

如果我有一个结构,例如:


struct Settings {

public string str;

public double doub1,doub2;

public int i1 ;

}


如何将i1的引用分配给变量?我可以通过将它作为ref int

传递给函数来轻松完成此操作,但有没有办法在不将其传递给函数的情况下执行此操作?


If I have a structure, for example:

struct Settings{
public string str;
public double doub1, doub2;
public int i1;
}

How do I assign the reference of i1 to a variable? I can easily do this by passing it as a ref int
to a function, but is there a way to do this without passing it to a function?

解决方案

On Nov 26, 4:44 pm, "Jon" <.wrote:

If I have a structure, for example:

struct Settings{
public string str;
public double doub1, doub2;
public int i1;

}

How do I assign the reference of i1 to a variable? I can easily do this by passing it as a ref int
to a function, but is there a way to do this without passing it to a function?

No. If you want a type to behave like a reference type, you should
make it a reference type to start with.

Jon


Jon wrote:

If I have a structure, for example:

struct Settings{
public string str;
public double doub1, doub2;
public int i1;
}

How do I assign the reference of i1 to a variable? I can easily do this by passing it as a ref int
to a function, but is there a way to do this without passing it to a function?

You can do it using unsafe code, but I wouldn''t recommend that. If you
use unsafe code, the syntax is very similar to C++.

Settings sets = new Settings();
sets.i1 = 5;
int *x = &sets.i1;
*x = 6;

Console.WriteLine(sets.i1.ToString());
--
Tom Porterfield


Thanks for your replies Jon + Tom.

I''m from a C background, but I don''t really want to use unsafe code.

"If you want a type to behave like a reference type, you should make it a reference type to start
with." How do I make an integer a reference type?

Jon
"Jon" <.wrote in message news:Oq*************@TK2MSFTNGP06.phx.gbl...
If I have a structure, for example:

struct Settings{
public string str;
public double doub1, doub2;
public int i1;
}

How do I assign the reference of i1 to a variable? I can easily do this by passing it as a ref int
to a function, but is there a way to do this without passing it to a function?


这篇关于分配对变量的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆