English 中文(简体)
这些选择与外地的简单接口有何差别?
原标题:What are the differences between these options for a simple interface to a field?
  • 时间:2011-11-07 15:57:10
  •  标签:
  • c#
  • oop
  • idioms

我在下面的法典中只是空洞的,并想知道是否确实需要填满12条源线。

    private static IUnityContainer _container;
    public static IUnityContainer Container
    {
        get
        {
            return _container;
        }
        set
        {
            _container = value;
        }
    }

我的想法是,为什么不仅仅是一个?

    public static IUnityContainer Container;

我认为,答案像“你能够打破cap头”。 这是否更是对条件的反应,还是还有其他一些原因、微妙或其他原因?

问题回答

确实,在你不希望以某种奇怪的方式,如GetType(......)”进行思考之前,没有任何缺点。

然而,从纯净的角度来说,财产通常比较清洁,这被认为难以揭开一个领域。

FxCop将警告你,这不是一件好事,但是没有缺点。

如果你想要做到简短但又干净,你可以公正使用自动财产:

public static IUnityContainer Container { get; set; }

然而,自编3版以来,自动财产才投入使用。

A possible drawback that can happen is if you or someone pass that field byref in some function, for example, Interlocked.Exchange(ref MyClass.MyStaticField, null); It will not work anymore if you change it with a property in the future, so you should be careful in not passing that field by reference. If you just use a property from the beginning you cannot have this problem. This problem cannot happen with static readonly fields, they cannot be passed by reference. Using static readonly fields is quite common.

绝对不能使用田地而不是使用地物的情况是,你有一等地继承沼泽地,用于重塑(RPC,远距离程序电话)。

我在此举了一个例子,正如我所说的那样,这不是你的情况,因为问题涉及的领域,而不是静态的领域。

public class MyClass :
    MarshalByRefObject
{
    public int MyValue;
}

class Program
{
    static void Main(string[] args)
    {
        var obj = new MyClass();

        // This will give you warning CS1690: Accessing a member on  MyValue  may cause a runtime exception because it is a field of a marshal-by-reference class
        Console.WriteLine(obj.MyValue.ToString());
    }
}

远距离程序电话只使用方法和特性,因此,汇编者向您发出警告,因为可在另一个申请人内或通过另一个程序或另一个电脑,例如通过TCP/IP。

Personally, I think of it this way. Let s say that at some later time, I want to establish an inavariant that "Container" is never null. If it s defined as a public field, the way to enforce this would be to seek out each and every client that uses it and put code in preventing it from being set to null. If I have it as a property, the same can be accomplished with something like:

private static IUnityContainer _container = new ContainerImpl();
public static IUnityContainer Container
{
    get
    {
        return _container;
    }
    set
    {
        _container = value ?? _container;
    }
}

或者,你可能会对无效价值提出一种例外,使其更为明确。 具体内容很重要,但cap角是.。

因此,我认为,这种反应不仅是一种千真万别的反应,而且是一种务实和合理的反应,特别是在面对静态关键词时。 这实际上只是一个全球性的变数。 至少激励全球国家使供应商能够在一定程度上控制全球国家,而不是分散对整个应用的控制,并相信/鼓励客户保持一致。

人们可以把它看作“Don t Repeat You”。 涉及某个领域的任何共同逻辑都必须在整个地点重复。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签