C#类库中的集合

C#类库中的集合,c#,C#,我编写了一个简单的.dll类库C#。“我的文件”获取主程序的值,并使用该值创建一个html文件。看看我的代码:(我正在描述它,以便您可以检查我是否有错误) 我在我的类中添加了这个参数: public class MyClass1 { public void HTMLGen(int number, string name, IList<Collection> collection) public类MyClass1{public void HTMLGen(整数、字符串名、IList集合

我编写了一个简单的.dll类库C#。“我的文件”获取主程序的值,并使用该值创建一个html文件。看看我的代码:(我正在描述它,以便您可以检查我是否有错误)

我在我的类中添加了这个参数:

public class MyClass1 { public void HTMLGen(int number, string name, IList<Collection> collection)
public类MyClass1{public void HTMLGen(整数、字符串名、IList集合)
因此,我创建了一个新的“var html2”,并希望将其添加到XElement.Parse中。这是我的全部代码:

public class MyClass1 { public void HTMLGen(int number, string name, IList<Collection> collection) {

var html1 = string.Format("<p>This is number: {0} and this is name: {1}</p>", number, name);
var html2 = string.Format("<p> /* value_one and value_two from Collection */ </p>");

var xDocument = new XDocument(
    new XDocumentType("html", null, null, null),
    new XElement("html",
        new XElement("head"),
        new XElement("body",
                XElement.Parse(html1), XElement.Parse(html2))));

var settings = new XmlWriterSettings
{
    OmitXmlDeclaration = true,
    Indent = true,
    IndentChars = "\t"
};

using (var writer = XmlWriter.Create(@"C:\Users\Desktop\test.html", settings))
{
    xDocument.WriteTo(writer);
} } }
public类MyClass1{public void HTMLGen(整数、字符串名、IList集合){
var html1=string.Format(“这是编号:{0},这是名称:{1}

”,编号,名称); var html2=string.Format(“/*集合中的值1和值2*/

”; var xDocument=新xDocument( 新的XDocumentType(“html”,null,null,null), 新的XElement(“html”, 新XElement(“head”), 新XElement(“身体”, XElement.Parse(html1),XElement.Parse(html2)); var设置=新的XmlWriterSettings { OmitXmlDeclaration=true, 缩进=真, IndentChars=“\t” }; 使用(var writer=XmlWriter.Create(@“C:\Users\Desktop\test.html”,设置)) { xDocument.WriteTo(writer); } } }

请看我的评论。我不知道,我如何才能将我集合的值添加到此变量html2。

首先,我不明白为什么需要创建一个名为集合的类,该类包含两个整数。您可以使用整数数组或列表。或者,如果您不仅有int,还有其他d,那么最好使用结构atatypes

假设您需要这个类,那么它为什么要继承IList呢?我根本看不到它的任何用法

无论如何,我只想回答你的问题:

var html2 = string.Format("<p> /* {0} and {1} from Collection */ </p>", collection.value_one, collection.value_two);
var html2=string.Format(“/*{0}和{1}来自集合*/

”,Collection.value\u one,Collection.value\u two);
或者,如果您的值有不同的类型,只需将其转换为字符串即可

var html2 = string.Format("<p> /* {0} and {1} from Collection */ </p>", collection.value_one.ToString(), collection.value_two.ToString());
var html2=string.Format(“/*{0}和{1}来自集合*/

”,Collection.value_one.ToString(),Collection.value_two.ToString());
也许您正试图实现这一点

public class MyClass1 
{ 

    public void HTMLGen(int number, string name,  IList<int> collection) 
    {
        var html1 = string.Format("<p>This is number: {0} and this is name: {1}</p>", number, name);
        string html2 = "";

        foreach (var item in collection)
        {
            html2 += item + " ";
        }

        html2 = "<p>" + html2 + "</p>";
        //The rest of the code
    }
}
公共类MyClass1
{ 
public void HTMLGen(整数、字符串名、IList集合)
{
var html1=string.Format(“这是编号:{0},这是名称:{1}

”,编号,名称); 字符串html2=“”; foreach(集合中的var项) { html2+=项目+“”; } html2=“”+html2+”

”; //代码的其余部分 } }
用法:

MyClass1 myClass1 = new MyClass1();
List<int> numbers = new List<int> { 4, 3, 21, 123, 6 };
myClass1.HTMLGen(10, "name", numbers);
MyClass1 MyClass1=newmyclass1();
名单编号=新名单{4,3,21,123,6};
myClass1.HTMLGen(10,“名称”,数字);
您可以使用以避免长字符串。格式方法用法如下所示

var html1 = $"<p>This is number: {number} and this is name: {name}</p>");
var html2 = $"<p> This is first value: {collection[0]} and this is second value: {collection[1]}</p>";
var html1=$”这是数字:{number},这是名称:{name}

”; var html2=$“这是第一个值:{collection[0]},这是第二个值:{collection[1]}

”;
你想用Collection类实现什么?我的'var html2'必须是动态的,因为我想使用foreach循环。首先,你的Collection类不会编译。其次,你有一个Ilist的Ilist,第三,还不清楚你想实现什么。请给我们一个更具体的例子我只知道我必须以这种方式创建一个集合,我必须输入这个var html2值​​从我的收藏中,我必须使用foreach循环,因为这个'var html2'必须是动态生成的。这就是全部,我正在尝试这样做。(我正在学习)你可能认为你“知道”但你几乎肯定是错的。要么你误解了如何写出好的c#,要么更可能你误解了别人给你的指示。回到基础上来,告诉我们你的要求是什么(例如能够传递一组整数)我们可以尝试帮助您实现这一点。使用两个
int
属性创建
IList
的实现几乎肯定不是您想要做的。
String.Format
不要求您对传入的变量调用
ToString
,它会自动执行,因此您的两个代码片段实际上可以执行此操作同样的事情。我不确定你认为第二个会给你什么安全性…我不明白。html2必须生成一个我的html代码。为什么你写了一个“”?是的,我忘了它是一个html字符串::P只是用P标记围绕它,像这样:html2=“

”+html2+“

”你能在帖子中编辑你的代码吗?这对我有很大帮助。好的,很好,但我有一个this:var html2=string.Format(“这是值一:{0},这是值二:{1}

”,collecton.value_-one,collection.value_-two);我想在我的foreach循环中动态生成此代码。它与您的代码一起工作吗?如果是,您能准确地显示此代码吗?OMFG。请不要误解我的意思,但我不是代码生成器。我已经向您显示了您要求的内容,您可以尝试思考其他内容。。仅供参考:您可以这样做,我将使用一个计数器变量,并将其添加为v类似这样的值:html2+=(n+1).ToString()+“th item:“+item+”;
MyClass1 myClass1 = new MyClass1();
List<int> numbers = new List<int> { 4, 3, 21, 123, 6 };
myClass1.HTMLGen(10, "name", numbers);
var html1 = $"<p>This is number: {number} and this is name: {name}</p>");
var html2 = $"<p> This is first value: {collection[0]} and this is second value: {collection[1]}</p>";