ArteS ArteS - 29 days ago 10
C# Question

How can I input a list into the collection?

Can't add to collection a list of assemblers in square brackets [ ] write error!
Here is the complete class with all the collections

public static Random rng = new Random();
public static StringBuilder builder = new StringBuilder();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}

public static void Generator()
{
List<string> myStrings = new List<string>();
myStrings.Add("[assembly: AssemblyTitle(\"TITLE\")]");
myStrings.Add("[assembly: AssemblyDescription(\"TITLE\")]");
myStrings.Add("[assembly: AssemblyCompany(\"TITLE\")]");
myStrings.Add("[assembly: AssemblyProduct(\"TITLE\")]");
myStrings.Add("[assembly: AssemblyCopyright(\"TITLE\")]");
myStrings.Shuffle();
foreach (var item in myStrings)
{
builder.Append(item);
builder.Append(" ");
}
}


And try to call

this.textBox1.Text = AssemblyGen.builder.ToString();
this.textBox2.Text = AssemblyGen.builder.ToString();
this.textBox3.Text = AssemblyGen.builder.ToString();
this.textBox5.Text = AssemblyGen.builder.ToString();


In the margin of the text box should get a random text

Answer

You have to escape the quotes in your string literal:

myStrings.Add("[assembly: AssemblyTitle(\"TITLE\")]");

Furthermore, as it has already been noted, there isn't clear why you are using a verbatim string, @. So I have removed it from the above code.

Comments