为什么不在C#中使用const ref? [英] Why not const ref params in C#?

查看:60
本文介绍了为什么不在C#中使用const ref?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前有一个话题,但没有回答,只有人问

你为什么要这样做......


假设我有一个巨大的对象,并希望将它传递给一个例程,

只需要读取权限。声明

接收例程肯定会很好:


void DoSomething(const ref HugeThing){

...

}


通过这种方式,您可以获得一个小型参考的性能优势,即
通过,但安全性知道你的物品将会保持不变

返回。


关键在于制作物品的副本是浪费的。

按值传递。传递引用也是不安全的,因为

调用的例程可以随意丢弃你的对象。


为什么这么简单和基本的功能从这种新的

语言中遗漏了吗?

There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let''s say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?

推荐答案

我认为const和ref的基本概念是互斥的。你知道其他任何语言都支持const ref吗?


我相信你已经考虑过这个了,但是你不能只编码你的

DoSomething()不修改HugeThing? :-)


Robert

"我的名字" <是ne ***** @ nc.rr.com>在留言中写道

news:90 ************************** @ posting.google.c om ...
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref''?

I''m sure you''ve considered this already, but couldn''t you just code your
DoSomething() not to modify HugeThing? :-)

Robert
"My Name" <ne*****@nc.rr.com> wrote in message
news:90**************************@posting.google.c om...
之前有一个话题,但没有答案,只有人问
你为什么要这样做......

让我说我有一个巨大的对象,并希望将它传递给一个只需要读取权限的例行程序。将
接收例程声明为:

void DoSomething(const ref HugeThing){
...
} >
通过这种方式,您可以获得通过的小参考的性能优势,但在返回时,知道您的对象的安全性将保持不变。

关键因为制作一个对象的副本并且通过价值传递是浪费的。传递引用也是不安全的,因为
被调用的例程可以随意丢弃您的对象。

为什么这样一个新的
语言?
There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let''s say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?



引用是不变的,但就像你说对象的状态不是。


public myMethod(myObject obj)

{

obj.SomeProperty =" changed; //更改对象状态

obj = new myObject; // obj现在是一个本地对象,当方法结束时,传入的obj恢复为你传递的内容

}


为了好玩我创建了一个只读宾语。首先定义一个类如下:


公共类ReadOnly

{

bool isReadOnly = true;


public ReadOnly()

{}


public ReadOnly(bool initialState)

{

isReadOnly = initialState;

}


public bool IsReadOnly

{

get {return isReadOnly; }

set {isReadOnly = value; }

}

}


现在定义一个只需要进行只读的类。这个类使用上面的类来使自己只读。

公共类垃圾()

{

private ReadOnly isReadOnly;

私有字符串数据;


//创建一个读/写对象

public Junk(string theData):this(theData,new ReadOnly( false))

{}

//创建一个具有给定初始状态的对象

public Junk(string theData,ReadOnly initialState)

{

isReadOnly initialState;

data = theData;

}

公共字符串数据

{

get {return data; }

set

{

Trace.Assert(!isReadOnly.IsReadOnly," ReadOnly Object");

data = value;

}

}

}


在您的代码中创建一个然后,ReadOnly对象的实例将传递给您希望为只读对象的构造函数。因为您控制此对象并且没有公共访问权限,所以被调用的方法无法对其进行任何更改。您可以将对象创建为可写对象,然后在将其传递给另一种方法之前进行所有更改,然后将其标记为只读。


例如:


ReadOnly test = new ReadOnly(true);

Junk huge = new Junk(test,test);

TestReadOnly(huge); //尝试更改巨大对象上的属性的方法,如果尝试更改对象的状态将失败


如果TestReadOnly成功,那么它并没有尝试更改任何东西。然后,您可以使用以下方法将对象更改为可写:

test.IsReadOnly = false;

huge.Data =更改;


我使用Trace.Assert来产生戏剧效果。您可以使用任何您想要的机制,例如抛出异常或忽略更改而不执行任何操作。


再次只是为了好玩。如果这是一个很好的编码实践,这是有争议的,但它可以确保没有任何东西搞砸你的对象的状态。


-

C Addison Ritchie,MCSD

Ritch Consulting,Inc。

" My Name"写道:
The reference is constant but like you say the object''s state is not.

public myMethod(myObject obj)
{
obj.SomeProperty = "changed"; // changes objects state
obj = new myObject; // obj is now a local object, when method ends the passed in obj reverts back to what you passed in
}

For fun I created a readonly object. First define a class like the following:

public class ReadOnly
{
bool isReadOnly = true;

public ReadOnly()
{ }

public ReadOnly(bool initialState)
{
isReadOnly = initialState;
}

public bool IsReadOnly
{
get { return isReadOnly; }
set { isReadOnly = value; }
}
}

Now define a class that you need to make read only. This class uses the above class to make itself read only.
public class Junk()
{
private ReadOnly isReadOnly;
private string data;

// creates a read/write object
public Junk(string theData) : this(theData, new ReadOnly(false))
{ }

// create an object with a given initial state
public Junk(string theData, ReadOnly initialState)
{
isReadOnly initialState;
data = theData;
}

public string Data
{
get { return data; }
set
{
Trace.Assert(!isReadOnly.IsReadOnly, "ReadOnly Object");
data = value;
}
}
}

In your code you create an instance of a ReadOnly object then pass to the constructor of your object that you wish to make read only. Because you control this object and there is no public access to it the called method cannot make any changes to it. You can create your object as writable, make all your changes then before passing it to another method you could mark it as readonly.

For example:

ReadOnly test = new ReadOnly(true);
Junk huge = new Junk("test", test);
TestReadOnly(huge); // method that tries to change a property on huge object, will fail if it tries to change the object''s state

If TestReadOnly succeeds then it didn''t try to change anything. You can then change the object to writable using:
test.IsReadOnly = false;
huge.Data = "changed";

I used Trace.Assert for dramatic effect. You could use any mechanism you''d like such as throwing an exception or just ignoring the change and do nothing.

Again just for fun. It''s debatable if this is a good coding practice or not but it would work to ensure nobody screws with your object''s state.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.
"My Name" wrote:
之前有一个主题,但没有答案,只有人问
你为什么要这样做......

让我们说我有一个巨大的对象,并希望将它传递给一个只需要读取权限的例程。将
接收例程声明为:

void DoSomething(const ref HugeThing){
...
} >
通过这种方式,您可以获得通过的小参考的性能优势,但在返回时,知道您的对象的安全性将保持不变。

关键因为制作一个对象的副本并且通过价值传递是浪费的。传递引用也是不安全的,因为
被调用的例程可以随意丢弃您的对象。

为什么这样一个新的
语言?
There was a topic on this earlier, but no answer, only people asking
"Why do you want to do this..."

Let''s say I have a HUGE object and want to pass it to a routine that
only needs read access to it. It would sure be nice to declare the
receiving routine to be something like:

void DoSomething(const ref HugeThing) {
...
}

This way you get the performance benefit of a small reference being
passed, but the safety of knowing your object will be unchanged upon
return.

The point being that it is wasteful to make a copy of the object and
pass by value. It is also unsafe to just pass a reference, as the
called routine would then be free to trash your object.

Why is such simple and basic functionality missing from such a new
language?





" Robert Misiak" < RM ***** @ users.cutthispartout.sourceforge.net>在消息中写道

新闻:DK ****************** @ newsread1.news.pas.earth link.net ...

"Robert Misiak" <rm*****@users.cutthispartout.sourceforge.net> wrote in message
news:DK******************@newsread1.news.pas.earth link.net...
我认为const和ref的基本概念是互斥的。你知道其他任何支持等同于'const ref'的语言吗?
C ++?


void myFunc(const BigClass& myBigClass);

常量引用。传递实际对象本身(就像通过指针一样)而不是复制,但它是常量。这是他们在高中学习递归时教给我的第一件事之一 - 如何避免堆栈溢出;)

我敢肯定你已经已经考虑过了,但是你不能只编码你的东西()不要修改HugeThing吗? : - )
I think that the basic concepts of const and ref are mutually exclusive. Do
you know of any other languages that support an equivalent to `const ref''? C++?

void myFunc(const BigClass &myBigClass);
A constant reference. Passes the actual object itself (as if by pointer) rather than making a copy, but it is constant. This is
one of the first things they taught me back in high school when studying recursion - how to avoid stack overflows ;)
I''m sure you''ve considered this already, but couldn''t you just code your
DoSomething() not to modify HugeThing? :-)




如果它不是你的DoSomething(也就是你正在写一个别人使用的库)怎么办。


-

Adam Clauss
ca ***** @ tamu.edu


这篇关于为什么不在C#中使用const ref?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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