北商大學 C# 上課筆記

驗證Value不能為null

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

namespace 驗證value不能為null
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Member member = new Member(" ");
        }
    }

    public class Member
    {
        private string _Name;
        public string Name 
        { 
            get 
            { 
                return _Name; 
            } 
            set 
            {
                if (string.IsNullOrWhiteSpace(value) == true) throw new Exception("字串不能為空白");
                if (value.Length > 30) throw new Exception("字串長度不能為30");
                _Name = value;
            } 
        }

        public Member(string name) 
        { 
            this.Name = name;
        }
    }
}
Last modification:July 18, 2023
If you think my article is useful to you, please feel free to appreciate