convivial.developer


Hi, I have a web method that that retrieves an 'Type[]' array. Next, it create a List<MyType> and then loops through the array and copy a data from it into List<MyType>. Here is example;
[Webmethod]
public MyType[] SomeWebmethod(Paratmers someParameters)
{
Type[] typeArray = retrieveData(someParameters);
List<MyType> mytypeList = new List<MyType>();

for (int i = 0; i < typeArray.Length; i++)
{
MyType mytype = new MyType();
// copy data into mytype converting data to MyType
mytypeList.Add(mytype);
}

return mytypeList.ToArray();
}
I read up on public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) and thought that I could perhaps use it. But I have an issue with List<T>().

The issue is that the method retriveData() returns back a 'Type[]' not a List<Type>. So, I would have to convert 'Type[]' to List<Type> using the followng line of code;
List<Type> typeList = new List<Type>(typeArray);
I was concerned that this would slow down the method and that maybe it is not worth the trouble to go through the conversion and maybe I should leave the web method as it is.

I envision the web method as follows;

[Webmethod]
public MyType[] SomeWebmethod(Parameters someParameters)
{
List<Type> typeList =
new List<Type>(retrieveData(someParameters)); // FEAR THAT THIS WILL BE SLOW
List<MyType> mytypeList = typeList.ConvertAll<MyType>(delegate(Type type)
{
MyType mytype = new MyType();
// copy data from type into mytype
return mytype;
});

return mytypeList.ToArray();
}

Or I can do it this way;
[Webmethod]
public MyType[] SomeWebmethod(Paratmers someParameters)
{
// initialize delegate in MyType
MyType.InitDelegate();

List<Type> typeList =
new List<Type>(retrieveData(someParameters)); // FEAR THAT THIS WILL BE SLOW
List<MyType> mytypeList =
typeList.ConvertAll<MyType>(MyType.converter);

// de-initialize delete in MyType
MyType.
ClrDelegate();

return mytypeList.ToArray();
}

Then inside of MyType class I do the following;
public class MyType
{
// Define member variables here

// Define converter delegate variable.
static Converter<Type, MyType> converter;

// Define static method to initialize converter
public static void InitDelegate()
{
converter = new Converter<Type, MyType>(Convert);
}

// Define a method to clear converter.
public static void ClrDelegate()
{
converter = null;
}

// Define a static method to perform actual conversion.
private static MyType Convert(Type type)
{
MyType mytype = new MyType();
// copy data from type into mytype.
return mytype;
}
}
I guess that the advantage of the second method is that the class MyType encapsulates the conversion. I don't know if this is better than first method or not.

I am worried that the line '... new List<Type>(retrieveData(someParameters));' might be slow because List<> has to go threw each element in the array return by retrieveData() method and this might be slow.

What do you guys think

Thanks,
Erica



Re: Visual C# General System.Collections.Generic.List


Re: Visual C# General System.Collections.Generic.List

earamsey

Hi guys, I decided to go with the following;

[WebMethod]
public ReturnType[] GetRecords(GetRecordsParameters getRecordsParameters)
{
RecordTypeServer[] listOfServerRecords = null;
GetAllServerRecords(getRecordParameters, out listOfServerRecords);
ReturnType[] returnRecords = null;
if(listOfServerRecords!= null)
{
returnRecords =
Array.ConvertAll< RecordTypeServer, ReturnType > (listOfServerRecords,
delegate(RecordTypeServer serverRecord)
{
return new ReturnType(serverRecord);
});
}

return returnRecords;
}

I decided on using the Array class because it is the base class of all array objects in C#. So, I figure it must be optimized and there is no overhead in converting to a List<Type>, is this a fair assumption . Also, I noticed that on the web some of the examples create a 'Converter<TInput,TOutput> converter' delegate object, however, I chose to do the above. Is there an advantage in defining a converter object and passing to ConvertAll() like this;

Converter< RecordTypeServer, ReturnType > converter;
converter = new Converter< RecordTypeServer, ReturnType >(ConvertToType);
returnRecords =
Array.ConvertAll< RecordTypeServer, ReturnType >(listOfServerRecords, converter);

(I would have to define ConvertToType(RecordTypeServer) method also). Is there any benefit in this When I 'google.com' none of the sites mentioned anything about best practices or why one method is preferred over the other. I guess all its looking for is the keyword delegate and it doesn't care about the signature

Well, thanks for reading my post and I hope that someone answers my question.

Thanks,

Erica