Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, October 12, 2012 1:59 AM
How can I convert List<String> to Object[]
My method :
Public Class Car
{
Public Car getdata (Car c, object[] o)
{
// something
}
}
I am passing list<string> to the above method as second parameter but second parameter accepts only object[], I can't modify method
I can pass String[] { , , , } but I have List<String>
I get this error after converting List.ToArray()
Argument 2: cannot convert from 'System.Collections.Generic.List<string>' to 'object[]'
All replies (4)
Friday, October 12, 2012 2:06 AM ✅Answered
hi, refer below code
List<String> strings = new List<string>() { "A", "B", "C" };
Car c = new Car();
c.getdata(c, strings.Cast<Object>().ToArray());
Friday, October 12, 2012 7:14 AM ✅Answered
@ sbmdude
Public Car getdata (Car c, object[] o)
First, object[] looks to me like a design smell because an object is far too general a type to be conceptually meaningful.
in OOP, we are trying to model the real world; in the real world, my spouse would never ask be to go to the supermarket to purchase and object.
Sample conversions:
Susan: please go to an object and buy an object.
Gerry: yes, darling.
Susan: please go to Loblaws and buy an object.
Gerry: yes, darling.
Loblaws is a store and an store is an object
the above conversations are not very helpful, compare:
Susan: please go to Foodland and buy "Death by Chocolate".
Gerry: yes, darling. {"Death by Chocolate" is an ice cream flavour.}
more examples:
Susan: where are you going?
Gerry: i'm going to an object to purchase some objects.
Susan: where are you going?
Gerry: i'm going to an WalMart to purchase food.
at WalMart one can purchase many objects, for example, televisions and clothing.
Susan: where are you going?
Gerry: i'm going to an WalMart to purchase potato chips and bananas.
some might argue that potato chips are not food; others might claim that potato chips are a vegetable.
another point ... c and o are generally not useful variable names; some computer languages only allow single character variable names; c# is more flexible.
compare:
Public Car getdata (Car c, object[] o)
with
Public Car getParts (Car newModels, object[] engineParts)
generally, one's code should be self-documenting ... of course there are generic exceptions ... for example, if we're are working with .html, aspx, .css, et cetera, we might name our variable files, OTOH, where appropriate, for example if we are creating .txt and .pdf output for reporting, we might name our variable reports.
MORE INFORMATION
I can't modify method
okay, this happens ... so we grumble and look for a solution.
fortunately, as pointed out by Karthick (karthicks), Microsoft has provided the http://msdn.microsoft.com/en-us/library/bb341406.aspx "Enumerable.Cast<TResult> Method".
public static IEnumerable<TResult> Cast<TResult>(
this IEnumerable source
)
at the above link, study the code example that "demonstrates how to use Cast<TResult>(IEnumerable) to enable the use of the standard query operators on an ArrayList."
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("apple");
fruits.Add("mango");
Note: the Cast<TResult>(IEnumerable) example is contrived because this works too ...
foreach (String fruit in fruits) { Console.WriteLine(fruit); }
g.
Friday, October 12, 2012 2:32 AM
hello, thanks for the reply, I have List<String> collection and I don't know the values to pass them as String Array, thats the issue..
Friday, October 12, 2012 2:55 AM
The Karthick code is showing you how to do that.
First you cast to a list of object and then you convert to an array.