string - how to declare multiple variable names with loop in c# -
how can declare multiple variable names within loop in c#
is there way declare n strings loop
i don't have use array..please if can done without array please tell helpful me
for example:-
string st1 = ""; string st2 = ""; string st3 = ""; string st4 = ""; string st5 = "";
if there way please help
well, if interpreting question right, can declare array
or list
, initialize these elements
in loop
for example (array) (if want fix number of elements):
int n = 10; // number of strings string[] str = new string[n]; // creates string array of n elements (int = 0; < n; i++) { str[i] = ""; // set value "" @ position in array }
(list) (if don't want fix number of elements)
using system.collections.generic; ... int n = 10; list<string> str = new list<string>(); // creates list of strings // list<string> str = new list<string>(n) set number can hold (better performance) (int = 0; < n; i++) { list.add(""); // if you've set initial capacity list, aware elements go after pre allocated elements } list[0] = "hello world"; // how use list list[list.count - 1] = "i last element"; // list.count total amount of elements in list, , minus 1 fix indexing
Comments
Post a Comment