Skip to main content
 首页 » 编程设计

c#之访问列表自定义类中的项目

2024年02月20日8jpfss

好的,我有这个代码:

URLs.Add(new URL(str.URL, str.Title, browser)); 

这是 URL 类:

public class URL 
{ 
    string url; 
    string title; 
    string browser; 
    public URL(string url, string title, string browser) 
    { 
        this.url = url; 
        this.title = title; 
        this.browser = browser; 
    } 
} 

现在,我如何访问 URL 标题..?
即,URL 的属性 [0]...?当我打印 URLs[0].ToString 时,它只给我 Namespace.URL。
如何打印 URL 类中的变量?

请您参考如下方法:

升级您的类以公开公共(public)属性:

 public class URL  
    {  
        public string Url { get; set; }  
        public string Title { get; set; }  
        public string Browser { get; set; }  
        public URL(string url, string title, string browser)  
        {  
            this.Url = url;  
            this.Title = title;  
            this.Browser = browser;  
        }  
    }  

然后像这样访问您的属性:

foreach(var url in URLs) 
{ 
  Console.WriteLine(url.Title); 
}