Answered by:
Assign run time value to Anonymous array

Question
-
User2057092666 posted
hi all
-
var data3 = new[] { 1, 2, 3 }
we all know above one is Anonymous array
can we assign above Anonymous array values as runtime.
is it possible in c#?
Tuesday, November 18, 2014 4:42 AM -
Answers
-
User-760709272 posted
var data3 = lstClm.ToArray();
Don't be confused by "var". Just do
string[] data3
var does not making something anonymous, in the case of what you're doing Visual Studio works out the type for you. It can work this out from
var x = new []{1,2,3}
because it knows it is an array of ints, but it can't work it out from
var x = new [];
However the list you are getting data from is string so your variable has to be an array of string so just declare it as such.
string[] x;
People use var wrongly all the time and in situations like this is can cause problems.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 18, 2014 6:03 AM
All replies
-
User-760709272 posted
It's not an anonymous array, it's simply an int array
int[]
What do you mean by assign as runtime?
Tuesday, November 18, 2014 5:02 AM -
User2057092666 posted
thanks for your reply
actually i have one list
List<string> lstClm = new List<string>();
lstClm.Add("institutionid");
lstClm.Add("institutionname");
lstClm.Add("displayname");
lstClm.Add("shortname");
lstClm.Add("supportedfield");
lstClm.Add("schemename");
lstClm.Add("subscriptionto");i want to assign these list values to that array variable one by one through for loop.
-
var data3 = new[]
-
{
- for(int i=0;i<lstClm.length;i++)
- {
-
lstClm[i],
-
}
- }
is it possible?
finally i want like this
data3 = new[] { "institutionid", "institutionname","displayname","shortname","supportedfield","schemename","subscriptionto"}
Tuesday, November 18, 2014 5:56 AM -
-
User-760709272 posted
var data3 = lstClm.ToArray();
Don't be confused by "var". Just do
string[] data3
var does not making something anonymous, in the case of what you're doing Visual Studio works out the type for you. It can work this out from
var x = new []{1,2,3}
because it knows it is an array of ints, but it can't work it out from
var x = new [];
However the list you are getting data from is string so your variable has to be an array of string so just declare it as such.
string[] x;
People use var wrongly all the time and in situations like this is can cause problems.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 18, 2014 6:03 AM