簡體   English   中英

具有邏輯的C#專用設置器

[英]C# Private Setter with Logic

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

namespace Crystal_Message
{
    class Person
    {
        private string firstName="";
        private string lastName= "";
        private string phone="";


        public Person(string firstName, string lastName, string phone)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.PhoneNumber = phone;


        }

        public string FirstName
        {
            get { return firstName; }

            set
            {
                if (string.IsNullOrWhiteSpace(this.firstName)){

                    throw new ArgumentNullException("Must Include First Name");
                }

                this.firstName = value;
            }

        }

        public string LastName
        {
            get { return lastName; }

            set
            {
                if (string.IsNullOrWhiteSpace(lastName)){

                    throw new ArgumentNullException("Must Include Last Name");
                }

                this.lastName = value;
            }

        }

        public string PhoneNumber
        {
            get { return phone; }

            set
            {
                if (string.IsNullOrWhiteSpace(phone)){

                    throw new ArgumentNullException("Must Include Phone Number");
                }

                this.phone = value;
            }

        }


        public override string ToString()
        {
            return "First Name: " + this.FirstName + " " + " Last Name: " + this.LastName + " Phone Number: " + this.PhoneNumber;
        }


    }
}

我有一個簡單的人類,它包含名字,姓氏和電子郵件。 它對我有用,並且不會被細分。 我正在構建一個小型應用程序,並且該人員類是合適的。

我唯一的問題是,如何使用內部邏輯實現私有設置器,以確保該值不為null或為空?

您可以對屬性設置器使用private修飾符:

public string FirstName
{
    get { return firstName; }

    private set
    {
        // Here you must check if value is not null or empty, not the field
        if (string.IsNullOrWhiteSpace(value)){
            throw new ArgumentNullException("Must Include First Name");
        }
        this.firstName = value;
    }
}

在這種情況下,只能從同一類訪問屬性的設置器。

只將二傳手設為私人。 另外,您還有一個問題:

string.IsNullOrWhiteSpace(lastName)

將讀取現有屬性值。 您應該改為檢查新提供的value

string.IsNullOrWhiteSpace(value)

最終代碼:

    public string LastName
    {
        get { return lastName; }

        private set
        {
            if (string.IsNullOrWhiteSpace(value)) {
                throw new ArgumentNullException("Must Include Last Name");
            }

            lastName = value;
        }
    }

有兩種方法:

首先,您可以擺脫set而您的類可以對基礎值進行操作:

public string FirstName
    {
        get { return firstName; }
    }
}

// Then in your code:
this.firstName = "whatever"; // the local variable

或者,您可以將設置器的范圍設置為private

public string FirstName
    {
        get { return firstName; }

        private set
        {
            if (string.IsNullOrWhiteSpace(this.firstName)){

                throw new ArgumentNullException("Must Include First Name");
            }

            this.firstName = value;
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM