相关疑难解决方法(0)

为什么在结构的构造函数中设置属性不起作用?

我有以下不允许的代码(下面的错误),为什么?

    struct A
    {
        private int b;

        public A(int x)
        {
            B = x;
        }
        public int B
        {
            get { return b; }
            set { b=value; }
        }

    }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

在将所有字段分配给字段之前,不能使用'this'对象'Test.x'必须在控制返回给调用者之前完全分配

c# struct properties

6
推荐指数
2
解决办法
1384
查看次数

在C#中为结构分配值列表?

我有一个结构(.NET 3.5):

struct ColumnHeadings 
        { 
            public string Name ; 
            public int Width ; 
        } ;
Run Code Online (Sandbox Code Playgroud)

当我尝试为该结构分配值列表时,我得到一个'无法隐式转换类型字符串/ int到...':

private void doSomething()
{
    ColumnHeadings[,] ch = new ColumnHeadings[,]{{"column1",100},
                {"column2",100},{"column3",100}};
}
Run Code Online (Sandbox Code Playgroud)

结构值是否可以与多维数组相同的方式分配?或者我需要使用?分配值:

ch.Name = "column 1";
Run Code Online (Sandbox Code Playgroud)

更新:

感谢Marc的出色反馈,正确的解决方案是:

结构:

struct ColumnHeadings
        {
            private readonly string name;
            private readonly int width;
            public string Name { get { return name; } }
            public int Width { get { return width; } }
            public ColumnHeadings(string name, int width)
            {
                this.name = name;
                this.width = width;
            } …
Run Code Online (Sandbox Code Playgroud)

c# struct multidimensional-array .net-3.5

5
推荐指数
1
解决办法
2382
查看次数

使用 ref 关键字传递给方法时,结构体是否被装箱?

有谁知道当使用 ref 关键字将结构传递给方法时是否会发生装箱

struct Test
{
    public int Value;
}

static void Main()
{
    Test test = new Test();
    SomeMethod(ref test);
    System.Diagnostics.Debug.WriteLine(test.Value);
}

static void SomeMethod(ref Test test)
{
    test.Value = 5;
}
Run Code Online (Sandbox Code Playgroud)

我想对类型使用结构,但我有几个方法需要更改它。如果它装箱,我只会将其传递并返回它,或者可能使用一个类来代替。

c# struct ref parameter-passing

5
推荐指数
1
解决办法
2371
查看次数

如何在struct中初始化char数组

如何在C#中初始化结构中的char数组我正在这样做

struct cell
{
    public char[] domain =new char[16];
    public int[] Peers;
    public int NumberOfPeers;
    public char assignValue;
}
Run Code Online (Sandbox Code Playgroud)

但它给出的错误是我们无法初始化结构体中的数组!任何人都可以说出正确的方法

.net c# arrays struct initialization

5
推荐指数
2
解决办法
2万
查看次数

为索引器集访问器调用生成的 CS1612(无法修改返回值...)

为什么 C# 编译器会为尝试对不可变属性调用索引器集访问器的代码生成错误 CS1612?

using System;
using System.Collections.Generic;
using System.Text;

namespace StructIndexerSetter
{
    struct IndexerImpl {
        private TestClass parent;

        public IndexerImpl(TestClass parent)
        {
            this.parent = parent;
        }

        public int this[int index]
        {
            get {
                return parent.GetValue(index);
            }
            set {
                parent.SetValue(index, value);
            }
        }
    }

    class TestClass
    {
        public IndexerImpl Item
        {
            get
            {
                return new IndexerImpl(this);
            }
        }

        internal int GetValue(int index)
        {
            Console.WriteLine("GetValue({0})", index);
            return index;
        }
        internal void SetValue(int index, int value)
        {
            Console.WriteLine("SetValue({0}, {1})", index, value);
        } …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
1
解决办法
7114
查看次数

Matrix 4x4对象的结构或类

我正在尝试实现简单的软件3D引擎.所以我需要一个矩阵4x4来表示变换.我的矩阵的每个单元格类型都是System.Double格式,这意味着整个矩阵将使用8个字节*16个单元格= 128个字节.目前我已将此作为一个类实现.此矩阵也是不可变类型.
所以现在,当我试图通过这个矩阵乘以向量时,我正在做这样的事情:matrix.m11*vector.X(etc ...).我认为,要访问字段m11,程序首先需要获取'矩阵'参考值,然后检查它是否为null,然后找到m11值,是吗?如果我将类更改为struct,我的乘法会变得更快?我知道128字节结构很大,但如果我编写只使用ref/out keyowrds的方法?这将是很好的选择?
正如我在SharpDX库中看到的,矩阵是一个结构.我应该将类更改为struct然后提高性能吗?

c# performance

5
推荐指数
1
解决办法
1840
查看次数

.NET中的值类型,不变性(好)和可变性(邪恶)

我最近一直在阅读很多关于价值类型和参考类型及其差异的文献.这个问题围绕着价值类型的可变性不变性这一主题.

基于我所读到的,似乎.NET中的值类型应该以它们不可变的方式编写; 也就是说,一旦为它们分配了一个值,该类型在内存中的值永远不会改变.只有该类型的后续副本可以在内存中构造新实例,并使用基于原始值的新值.似乎.NET中的可变性是邪恶的.

为了澄清对不变性的理解(为了我自己的理智和其他人),我在下面证明了这一点:

DateTime并且TimeSpan不可变结构的示例,因为一旦将值赋给实例,该实例值就不会改变,这通过只读属性很明显:

DateTime dt = new DateTime();
DateTime newdt = dt.AddDays(2); // Okay, new value stored in newdt
newdt.Year = 1945; // Error, cannot write to readonly property
Run Code Online (Sandbox Code Playgroud)

然而,当看到原始类型时,不变性可能会令人困惑Int32,Double或者Char,因为类型似乎是可变的,但我的直觉是实际上,通过CLR透明地处理不变性; 以下是一些操作(我在一些非常基本的x86中进行了评论,以了解如何根据原始类型处理不变性)

int x = 0;

// xor eax, eax;     'clear register to 0
// push eax;         'push eax (0) onto the stack

x = …
Run Code Online (Sandbox Code Playgroud)

.net c# struct mutable immutability

5
推荐指数
1
解决办法
659
查看次数

如何确保通用的不变性

这个例子在C#中,但问题确实适用于任何OO语言.我想创建一个实现IReadOnlyList的通用不可变类.此外,此类应具有无法修改的基础通用IList.最初,该课程编写如下:

public class Datum<T> : IReadOnlyList<T>
{
    private IList<T> objects;
    public int Count 
    { 
        get; 
        private set;
    }
    public T this[int i]
    {
        get
        {
            return objects[i];
        }
        private set
        {
            this.objects[i] = value;
        }
    }

    public Datum(IList<T> obj)
    {
        this.objects = obj;
        this.Count = obj.Count;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
    public IEnumerator<T> GetEnumerator()
    {
        return this.objects.GetEnumerator();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不是一成不变的.正如您可能知道的那样,更改初始IList'obj'会更改Datum的'对象'.

static void Main(string[] args)
{
    List<object> list = new List<object>();
    list.Add("one");
    Datum<object> datum = new Datum<object>(list);
    list[0] = …
Run Code Online (Sandbox Code Playgroud)

c# generics immutability

5
推荐指数
1
解决办法
894
查看次数

结构和接口中的C#getters/setter

我来了(根据我)C#中的结构和接口之间的奇怪区别.考虑这个接口和结构:

public interface INumber
{
    void ChangeNumber(int n);
    void Log();
}
public struct Number : INumber
{
    private int n;
    public void ChangeNumber(int n)
    {
        this.n = n;
    }
    public void Log()
    {
        Console.WriteLine(this.n);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我创建一个具有Number作为属性的新类时,使用ChangeNumber方法将n更改为2并使用Log打印该数字,它将打印0:

public class NumberContainer
{
    public Number Number { get; set; }
    public NumberContainer()
    {
        this.Number = new Number();
        this.Number.ChangeNumber(2);
        this.Number.Log();              //prints 0...
    }
}
Run Code Online (Sandbox Code Playgroud)

过了一会儿,我意识到这是因为当我调用时this.Number.ChangeNumber(2);,我实际创建了一个新对象(因为getter)并将该数字更改为2.但后来我通过将Number属性更改为INumber属性来更改了一些代码:

public class NumberContainer
{
    public INumber Number { get; set; }
    public NumberContainer()
    {
        this.Number = …
Run Code Online (Sandbox Code Playgroud)

c# getter struct interface

5
推荐指数
1
解决办法
4962
查看次数

条件运算符的引用语义

作为条件运算符()的结果,我正在使用左值cond ? expr1 : expr2.

考虑以下示例

class /*(1)*/ Value {
    public int MagicNumber { get; private set; } = 0;
    public void Increment() {
        /* magical code, that modifies MagicNumber */
    }
}

void Main()
{
    Value v1, v2;
    /*(2)*/ bool condition = /*...*/;
    (condition ? v1 : v2).Increment(); // (3)
}
Run Code Online (Sandbox Code Playgroud)

现在,我会怀疑,基于价值condition,要么v1还是v2被递增.实际上就是这种情况,只要Value((1))是类.一旦我将其更改为struct,它就变成了值类型而行(3)没有做任何事情(我怀疑是因为其中一个v1或被v2复制,递增和丢弃).到此为止,这是有道理的.奇怪的行为开始一次(2)(condition)在编译时就已知(例如通过定义它 …

c#

5
推荐指数
1
解决办法
117
查看次数