简体   繁体   English

C#6.0至<6.0

[英]C# 6.0 to < 6.0

I need to convert the following code which is in C# 6.0 to a lower version which works in .NET framework 4.5. 我需要将C# 6.0的以下代码转换为在.NET Framework 4.5中工作的较低版本。

  public ImageCollection Strokes { get;  } = new ImageCollection();  //C# 6.0

How do i convert this? 我该如何转换呢?

Tried the below code but had few issues in the application. 尝试了以下代码,但应用程序中的问题很少。

  public ImageCollection Strokes
        { get { return new ImageCollection(); } }   //< 6.0

Kindly help. 请帮助。 Also is there any way to convert these? 还有什么办法可以转换这些? Often i get solutions in 6.0 which i have to downgrade. 我通常会在6.0中获得必须降级的解决方案。

You are going to have to assign the property in your constructor. 您将必须在构造函数中分配属性。

Option 1 - A property with a private setter: 选项1-具有私人二传手的财产:

class Example
{
    public ImageCollection Strokes { get; private set; }

    public Example()
    {
        Strokes = new ImageCollection();
    }
}

Option 2 - A property with a getter only, backed by a private readonly field: 选项2-仅具有吸气剂的属性,由私有只读字段支持:

class Example
{
    public ImageCollection Strokes { get { return _strokes; } }

    private readonly ImageCollection _strokes;

    public Example()
    {
        _strokes = new ImageCollection();
    }
}

Option 3 - Like option 2, but assigning the field inline instead of in the constructor: 选项3-与选项2类似,但直接在内部而不是在构造函数中分配字段:

class Example
{
    public ImageCollection Strokes { get { return _strokes; } }

    private readonly ImageCollection _strokes = new ImageCollection();
}

Its not the same. 这是不一样的。

In the first example you have a property which will be instantiated through the constructor. 在第一个示例中,您具有一个将通过构造函数实例化的属性。

In the second example you return a new instance every time you access the property. 在第二个示例中,每次访问属性时,您都会返回一个新实例。

The correct refactor looks like this: 正确的重构如下所示:

public ImageCollection Strokes
{ 
    get; private set; 
} 

public constructor() 
{
    Strokes = new ImageCollection();
}

You could create a private field that the getter uses: 您可以创建getter使用的私有字段:

class MyClass
{
    private ImageCollection _strokes = new ImageCollection();
    public ImageCollection Strokes { get { return _strokes }  }
 }

Or you could initialize it in the constructor. 或者,您可以在构造函数中对其进行初始化。

class MyClass
{
    public ImageCollection Strokes { get; private set; }

    public MyClass()
    {
          Strokes = new ImageCollection();
    }
}

Just remember to initialize it in all constructors of the class, or at least have your other constructors call a constructor that does. 只需记住在类的所有构造函数中对其进行初始化,或者至少让您的其他构造函数调用该构造函数即可。


The problem with your solution is that it returns a new instance of ImageCollection each time get is called. 解决方案的问题在于,每次调用get ,它都会返回ImageCollection的新实例。 This produces multiple that have nothing to do with each other. 这产生了彼此无关的多个。 Changing one will not change the rest. 换一个不会改变其余的。

I dont know what issues you arr facing but change code like below your code always creating the new object , but 6.0 one time initialization Try 我不知道您会遇到什么问题,但是更改代码(如下面的代码)总是创建新对象, 但是6.0一次初始化尝试

var imgCollection = new ImageCollection(); 

public ImageCollection Strokes
    { get { return imgCollection; } }   //< 6.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM