任何对象的ref参数 [英] ref parameter for any object

查看:56
本文介绍了任何对象的ref参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望有一个带有ref对象参数的方法,这样我就可以通过引用传递任何

对象,并在其中写入输出,如下面的代码所示。问题是

它在c#中不起作用,就像我编码一样。我不想使用重载!!!

有可能吗?如果有,怎么样?

thanx jazper


---代码---

public void foo(ref object o)

{

if(o is int)((int)o)= 1;

else if(o是double)((double )o)= 3.5;

else if(o是string)((string)o)=" test" ;;

else throw new Exception(" type not认可!");

}

public void callfoo()

{

int i = 0;

double d = 0.0;

string s = string.Empty;

foo(ref(object)i);

foo(ref(object)d);

foo(ref(object)s);

}

hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!! is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}

推荐答案

Jazper,


这里有一些事情发生。你应该在调用foo时编译

错误,因为你必须匹配签名(对象违反它的演员

)。

另外,当你这样做时:


((int)o)= 1;


这也是一个编译错误。为了使这项工作,您需要

执行以下操作:

public void foo(ref object o)

{

if(o is int)

o = 1;

else if(o是double)

o = 3.5;

否则if(o是字符串)

o =" test" ;;

else

throw new例外(类型无法识别!);

}


public void callfoo()

{

int i = 0;

double d = 0.0;

string s = string.Empty;


//具有赋值的对象。

object o = null;


//设置o,然后调用。

o = i;

foo(ref o);


//设置o,然后调用。

o = d;

foo(ref o);


//设置o,然后调用。

o = s;

foo(ref o);

}


这是它的唯一工作方式。根据你在foo中实际做的是什么
,Generics可能会对.NET 2.0有帮助(现在是测试版

)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Jazper Manto" < EJ ***** @ hotmail.com>在留言中写道

news:um ************** @ TK2MSFTNGP09.phx.gbl ...
Jazper,

There are a few things going on here. You should be getting compile
errors in the calls to foo, because you must match the signature (the cast
to object violates it).

Also, when you do this:

((int) o) = 1;

That is also a compile error. In order to make this work, you need to
do the following:

public void foo(ref object o)
{
if( o is int)
o = 1;
else if( o is double)
o = 3.5;
else if( o is string)
o = "test";
else
throw new Exception("type not recognized!");
}

public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;

// An object that has the assignment.
object o = null;

// Set o, then call.
o = i;
foo(ref o);

// Set o, then call.
o = d;
foo(ref o);

// Set o, then call.
o = s;
foo(ref o);
}

This is the only way it is going to work. Depending on what you are
actually doing in foo, Generics might help in .NET 2.0 (which is in beta
right now).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
我想有一个带有ref对象参数的方法,以便我可以通过引用传递任何
对象并在其中写入输出,如下面的代码。问题是
它在c#中不起作用,就像我编码一样。我不想使用超载!!!
有可能吗?如果是的话,怎么样?
比起jazper

--- CODE ---
public void foo(ref object o)
{
if(o是int)((int)o)= 1;
else if(o是double)((double)o)= 3.5;
else if(o是string)((string)o)= test;
抛出新的异常(类型无法识别!);
}
public void callfoo()
{i /> int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref(object)i);
foo(ref(object)d);
foo(ref(object)s);
}
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!!
is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}



试试这个....


public void foo(ref object o)

{

if(o is int)o = 1;

否则if(o为double)o = 3.5;

else if(o is string)o =" test" ;;

else throw new Exception(" type not认可!");

}

public void callfoo()

{

int i = 0;

double d = 0.0;

string s = string.Empty;

object o = i;

foo(ref o);

o = d;

foo(ref o);

o = s;

foo(ref o);

}


Willy。


" Jazper Manto" < EJ ***** @ hotmail.com>在留言中写道

news:um ************** @ TK2MSFTNGP09.phx.gbl ...
Try this....

public void foo(ref object o)
{
if( o is int) o = 1;
else if( o is double) o = 3.5;
else if( o is string) o = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
object o = i;
foo(ref o);
o = d;
foo(ref o);
o = s;
foo(ref o);
}

Willy.

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
我想有一个带有ref对象参数的方法,以便我可以通过引用传递任何
对象并在其中写入输出,如下面的代码。问题是
它在c#中不起作用,就像我编码一样。我不想使用超载!!!
有可能吗?如果是的话,怎么样?
比起jazper

--- CODE ---
public void foo(ref object o)
{
if(o是int)((int)o)= 1;
else if(o是double)((double)o)= 3.5;
else if(o是string)((string)o)= test;
抛出新的异常(类型无法识别!);
}
public void callfoo()
{i /> int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref(object)i);
foo(ref(object)d);
foo(ref(object)s);
}
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!!
is
it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}






返回它并不容易吗?

公共对象foo(对象o)


无论如何,你的代码有几个东西(复杂和基础知识) )参与我的b $ b将尝试解释。


拳击和拆箱

---------- ----------------

当你将一个值类型转换为引用类型时ie:int to object you

在堆中创建一个新的对象实例。用

更好地解释一个例子

double d = 0.0;

object o = d;

d = 4;


现在你在o引用的堆中有两个双倍的另一个在

堆栈中d ,因此

d ==(双)o是假的


现在当你说foo(ref(object)i)你正在创建一个新对象时

堆(装箱)int。


发生的事情是使用ref参数你不能这样做,一个ref参数

需要是一个左值,这意味着它应该是一个有效的表达式,如

左侧的分配部分,如:

(对象)i = 4;

这是无效的(你在使用的if / else

构造中遇到同样的问题。


这是必要的原因否则所使用的参考文件在返回时会丢失,请认为

关于它,您正在创建一个新实例并且您没有保留任何

引用它。!!! />
我认为你会得到几个答案,可能是一个用比我更好的书面技术英语的

.


干杯,


-

Ignacio Machin,

ignacio.machin at dot.state.fl。我们

佛罗里达州交通局


" Jazper Manto" < EJ ***** @ hotmail.com>在留言中写道

news:um ************** @ TK2MSFTNGP09.phx.gbl ...
Hi,

It is not easier to just return it?
public object foo( object o )

Anyway, your code has several things (complex and basics) involved that I
will try to explain.

Boxing and unboxing
--------------------------
when you convert a value type to a reference type ie : int to object you
create a new instance of object in the heap. this is better explained with
an example
double d = 0.0;
object o = d;
d=4;

now you have TWO double one in the heap referenced by "o" and another in the
stack "d" , therefore
d == (double)o is FALSE

now when you say foo(ref (object)i) you are CREATING a new object in the
heap (boxing) the int.

what happen is that with a ref parameter you cannot do this, a ref parameter
NEEDS to be a lvalue, meaning that it should be a expression valid in the
left part of an assignation like:
(object)i=4;
which is not valid ( you are getting the same problem in the if/else
construct you are using.

this is needed cause otherwise the reference used is lost at return , think
about it, you are creating a new instance and you are NOT keeping any
reference to it. !!!
I think that you will get several answer to this, probably one with a
better written techical english than me :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jazper Manto" <ej*****@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
hi
我想有一个带有ref对象参数的方法,以便我可以通过引用传递任何
对象并在其中写入输出,如下面的代码。问题是
它在c#中不起作用,就像我编码一样。我不想使用重载!!!
有可能吗?如果是的话,怎么样?
比起jazper

--- CODE ---
public void foo(ref object o)
{
if(o是int)((int)o)= 1;
else if(o是double)((double)o)= 3.5;
else if(o是string)((string)o)= test;
抛出新的异常(类型无法识别!);
}
public void callfoo()
{i /> int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref(object)i);
foo(ref(object)d);
foo(ref(object)s);
}
hi
i want to have a method with a ref object parameter so that i can pass any
object by reference and write output in it like code below. the problem is
it does not work in c# like i coded it. i do NOT want to use overloads!!! is it possible? if yes, how?
thanx jazper

--- CODE ---
public void foo(ref object o)
{
if( o is int) ((int)o) = 1;
else if( o is double) ((double)o) = 3.5;
else if( o is string) ((string)o) = "test";
else throw new Exception("type not recognized!");
}
public void callfoo()
{
int i = 0;
double d = 0.0;
string s = string.Empty;
foo(ref (object)i);
foo(ref (object)d);
foo(ref (object)s);
}



这篇关于任何对象的ref参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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