Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问列表自定义类中的项_C# - Fatal编程技术网

C# 访问列表自定义类中的项

C# 访问列表自定义类中的项,c#,C#,好的,我有这个代码: 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 = t

好的,我有这个代码:

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]的属性。。。?当我打印URL[0].ToString时,它只给我名称空间.URL。

如何打印URL类内部的变量?

有几件事-默认情况下,类的所有成员都是私有的-这意味着外部调用者无法访问它们。如果希望它们可用,请将它们标记为公共:

public string url;
然后你可以做:

URLs[0].url;
如果希望简化结构的管道,可以通过添加如下方法重写ToString:

public override string ToString()
{
    return string.format("{0} {1} {2}", url, title, browser);
}
然后简单地打电话:

URLs[0].ToString();

升级类以公开公共属性:

 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);
}

你没有提供足够的信息来回答这个问题。什么样的对象是
url
?@freefiller,他在标题-列表中说。