c# - 数据完整性问题C#

标签 c# variable-assignment overwrite

我的问题是下面的 decodedProxyExcerpt2 的分配覆盖了 decodedProxyExcerpt1,我不知道为什么。

有什么线索吗?

提前致谢。

        DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt1 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt1.data contains the correct values.

        DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt2 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt2.data contains the correct values.
        // However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data.


public class DecodedProxyExcerpt
{
    public short[] data { get; set; } // PCM data

    public DecodedProxyExcerpt(int size)
    {
        this.data = new short[size];
    }

}

来自 AudioFactory:

    public bool MoveNext(int stepSize)
    {
        if (index == -1)
        {
            index = 0;
            return (true);
        }
        else
        {
            index = index + stepSize;
            if (index >= buffer.Length - stepSize)
                return (false);
            else
                return (true);
        }
    }

    public DecodedProxyExcerpt Current(int stepSize)
    {
        Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize);
        return(CurrentExcerpt);
    }}

最佳答案

从它的外观来看,audiofactory.MoveNext(stepSize) 保持在相同的引用位置。这导致 audiofactory.Current(stepSize) 停留在同一地址。

因此,decodedProxyExcerpt1decodedProxyExcerpt2 指向相同的引用,因此对其中一个的更改会传播到另一个。

所以,问题出在您的 AudioFactory 类上。

关于c# - 数据完整性问题C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771891/

相关文章:

c# - 无法读取 Windows 8 中的注册表值

c# - 如何忽略 Quartz.Net 中的失火?

objective-c - 将文字字符串分配给具有 "="的 NSString 实际上有什么作用?

c++ - 局部变量赋值导致Audio在JUCE中停止处理

JavaScript - 覆盖 HTMLImageElement(s) 的 .onload 原型(prototype)

mysql - 如何从命令行导入 MySQL 转储 WITH 覆盖

c# - TextBlock 附加属性绑定(bind)

c# - 启动画面不会将焦点返回到主窗体

Python:为什么我不能在这种情况下为数组分配新值?

java - 用java制作一个可更新的程序?