这有什么不好ref参数? [英] What's so bad about ref parameters?

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

问题描述

我面对,我认为只能通过使用ref参数来解决的情况。然而,这将意味着改变一个方法总是接受ref参数的时候,我只需要通过ref参数的5%的时间提供的功能。

I'm faced with a situation that I think can only be solved by using a ref parameter. However, this will mean changing a method to always accept a ref parameter when I only need the functionality provided by a ref parameter 5% of the time.

这让我想到哇,疯狂,必须另谋出路。我是不是傻?

This makes me think "whoa, crazy, must find another way". Am I being stupid? What sort of problems can be caused by a ref parameter?

修改

被要求进一步的细节,我不认为他们是我在问什么完全相关,但在这里我们去。

Further details were requested, I don't think they are entirely relevant to what I was asking but here we go.

我想无论是保存一个新的实例(这将与可能在以后使用的ID更新),或检索符合一些逻辑和更新的是,保存它然后更改新实例的参考指向现有的现有实例

I'm wanting to either save a new instance (which will update with the ID which may later be used) or retrieve an existing instance that matches some logic and update that, save it then change the reference of the new instance to point to the existing one.

代码可能会更清楚:

protected override void BeforeSave(Log entity)
{
    var newLog = entity;

    var existingLog = (from log in repository.All()
                           where log.Stuff == newLog.Stuff 
                                 && log.Id != newLog.Id
                           select log).SingleOrDefault();

    if (existingLog != null)
    {
        // update the time
        existingLog.SomeValue = entity.SomeValue;
        // remove the reference to the new entity
        entity = existingLog;
    }
}

// called from base class which usually does nothing before save
public void Save(TEntity entity)
{
    var report = validator.Validate(entity);

    if (report.ValidationPassed)
    {
        BeforeSave(entity);
        repository.Save(entity);
    }
    else
    {
        throw new ValidationException { Report = report };
    }
}



这是事实,我会只添加它对于一个孩子(到目前为止)的基类,以防止使用我的过载(由于这一事实,我会重复保存方法)。我也有这个问题,由此,我需要迫使他们在这种情况下使用裁判的版本,否则预期的事情将无法正常工作。

It's the fact that I would be adding it in only for one child (so far) of the base class that prevents me using an overload (due to the fact I would have to duplicate the Save method). I also have the problem whereby I need to force them to use the ref version in this instance otherwise things won't work as expected.

推荐答案

你能添加一个过载?有没有裁判参数一个签名,一个与它。

Can you add an overload? Have one signature without the ref parameter, and one with it.

ref参数的可以的是有用的,我很高兴他们在C#中存在,但他们不应该被不假思索使用。通常,如果一个方法可以有效地返回两个值,这将是更好的任一的方法分为两部分,或者在一个单一型封装这两个值。每一种情况下,虽然这些都不盖 - 有一定时候, REF 是最好的选择。

Ref parameters can be useful, and I'm glad they exist in C#, but they shouldn't be used without thought. Often if a method is effectively returning two values, it would be better either to split the method into two parts, or encapsulate both values in a single type. Neither of these covers every case though - there are definitely times when ref is the best option.

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

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