C# 检查以确保我的对象不是';t部分初始化

C# 检查以确保我的对象不是';t部分初始化,c#,C#,我正在创建一个简单的应用程序,它接受一个URL和一个字符串,并为一个超链接创建代码 这是我的超链接课程 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinkIt_ { class HyperLink { private string url; private string text; public HyperLink()

我正在创建一个简单的应用程序,它接受一个URL和一个字符串,并为一个超链接创建代码

这是我的超链接课程

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

 namespace LinkIt_
{
class HyperLink
{
    private string url;
    private string text;

    public HyperLink()
    {

    }

    public HyperLink(string url,string text)
    {
        this.Website = url;
        this.Text = text;
    }


    public String Website
    {
        get
        {
            return url;
        }
        set
        {
            if (String.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("Must have URL!");
            }

            this.url = value;
        }


    }

    public String Text
    {

        get
        {
            return text;
        }

        set
        {
            if (String.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("Must have Text!");
            }
            this.text = value;
        }



    }

    public string addPoint()
    {

        return String.Format("<li><a href=" + "\" {0} \">", url) + text + "</a></li>";  
    }

    public override string ToString()
    {

        return String.Format("<a href=" + "\" {0} \">",url) + text + "</a>" ;   
    }



           }
       }
我的问题:

  • 如何确保不创建部分初始化的对象

  • 如果我的代码需要修改,有人能帮我吗


  • 您可以重构代码以使用访问器可见性降低的属性。 这样就不能从类外更改
    超链接
    对象(这通常是数据结构的首选属性)

    例如,您可以这样做:

    class HyperLink
    {
        public String Website{get; private set;}
    
        public String Text {get; private set;}
    
        public HyperLink(string url,string text)
        {
            if(string.isNullOrEmpty(url) || string.IsNullOrEmpty(text))
              throw new ArgumentNullException("no partially intialized object allowed");
    
            this.Website = url;
            this.Text = text;
        }
    
        public string AddPoint()
        {
            return String.Format("<li><a href=" + "\" {0} \">", url) + text + "</a></li>";  
        }
    
        public override string ToString()
        {
            return String.Format("<a href=" + "\" {0} \">",url) + text + "</a>" ;   
        }
    }
    

    快速提问。如果我将setter设置为公共,并使用默认的无参数构造函数,然后检查以确保表单中的文本框不是空的,并且实际上包含文本,可以吗?@Sylvia Rosemond这个问题很难回答。它可能适合您,但需要您在处理对象时小心。如果您按照我的方式操作,就不能使对象进入无效状态。@yas4891谢谢!如果你不介意的话,再来一杯。在我的方法中,我调用url/text来显示字符串。但是,我不能再这样做了,可以从我自己的类中调用getter/setter吗?例如,在toString方法中,它现在将读取返回String.Format(“我的基本问题,在同一个类中调用getter安全吗?”@SylviaRosemond我将对您的问题的答案合并到了我的原始答案中
    class HyperLink
    {
        public String Website{get; private set;}
    
        public String Text {get; private set;}
    
        public HyperLink(string url,string text)
        {
            if(string.isNullOrEmpty(url) || string.IsNullOrEmpty(text))
              throw new ArgumentNullException("no partially intialized object allowed");
    
            this.Website = url;
            this.Text = text;
        }
    
        public string AddPoint()
        {
            return String.Format("<li><a href=" + "\" {0} \">", url) + text + "</a></li>";  
        }
    
        public override string ToString()
        {
            return String.Format("<a href=" + "\" {0} \">",url) + text + "</a>" ;   
        }
    }
    
    String.Format("<a href=\"{0}\">{1}</a>",this.Link, this.Text);