C# 3 中的自动属性 ​​- 如果我为 set 声明一个主体,必须为 get 声明一个主体吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1121940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:43:13  来源:igfitidea点击:

Automatic Properties in C# 3 - Must declare a body for get if I declare one for set?

c#.net-3.5

提问by Matt

I'm using VS 2008, and in my property pages for the project I see that I'm targeting .Net 3.5.

我正在使用 VS 2008,在我的项目属性页中,我看到我的目标是 .Net 3.5。

Here is the error I'm getting when trying to compile:

这是我在尝试编译时遇到的错误:

AMSDataModels.Vehicle.VIN.get' must declare a body because it is not marked abstract, extern, or partial

AMSDataModels.Vehicle.VIN.get' 必须声明一个主体,因为它没有标记为抽象、外部或部分

And here is the code:

这是代码:

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

namespace AMSDataModels
{
    public class Vehicle
    {
        //NodeID for datastore persistance
        public Guid NodeID { get; set; }

        public string VIN { get; 
            set { 
                if (value.Length != 17) throw new ArgumentOutOfRangeException("VIN", "VIN must be 17 characters"); 
            } }

        public string Make { get; set; }
        public string Model { get; set; }
    }
}

If I strip the body from set so that its just:

如果我从集合中剥离身体,使其只是:

public string VIN { get; set; }

All works, but I lose my ability to check the VIN as it is set.

一切正常,但我无法在设置时检查 VIN。

Does anyone have a suggestion of how to fix this or a better way to approach the problem at hand?

有没有人对如何解决这个问题或解决手头问题的更好方法有任何建议?

I really like the shorthand notation - but verifying the legitimacy of input is important too!

我真的很喜欢速记符号 - 但验证输入的合法性也很重要!

采纳答案by Brandon

If you're going to add logic in the set, you need to add it into the get as well. Notice in your set you're not actually setting a value to anything?

如果要在集合中添加逻辑,则还需要将其添加到 get 中。请注意,在您的集合中您实际上并没有设置任何值?

Add a backing field,

添加一个支持字段,

private string _vin;

and return that in the get.

并在 get 中返回。

public string VIN
{
    get { return _vin; }
    set
    {
      if (value.Length != 17) 
        throw new ArgumentOutOfRangeException("VIN", "VIN must be 17 characters"); 
      else
        _vin = value;
    }
}

回答by Adrian Godong

Yes, you will have to declare get implementation as well. Oh, and your set code does not do anything other than validation. You will need to provide additional implementation for that as well, assuming that you want to set the value if it passes validation.

是的,您还必须声明 get 实现。哦,您的设置代码除了验证之外什么都不做。您还需要为此提供额外的实现,假设您希望在通过验证时设置该值。

If you need anything more than just basic get/set implementation, you will have to implement the whole property, not just the difference.

如果您需要的不仅仅是基本的 get/set 实现,您将必须实现整个属性,而不仅仅是差异。

回答by Paul Alexander

When automatic properties are used, the compiler automatically generates a backer field. When you declare your own, there's no way for it to know what field to use for the get method. So you have to declare both or none.

使用自动属性时,编译器会自动生成支持字段。当您声明自己的时,它无法知道用于 get 方法的字段。所以你必须声明两者或没有。

Incidentally, your current set method only checks for the value - it never actually assigns it to anything.

顺便说一句,您当前的 set 方法仅检查该值 - 它实际上从未将其分配给任何东西。

回答by flq

You'll have to use the good ol' backing field. The short-hand notation can't be mixed. The only extra fun is to change the access modifier on get and set, e.g. get; private set;

你将不得不使用好的 ol' 支持字段。不能混合使用简写符号。唯一额外的乐趣是在 get 和 set 上更改访问修饰符,例如 get; 私人订制;