By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

using generic types

P: 13
I have a function like
Expand|Select|Wrap|Line Numbers
  1. UInt32[] MyFunction(UInt32[] Inputs, bool bOrder)
Instead of rewriting the whole function for UInt64[] and for string[] I would like to write it in a generic way using something like
Expand|Select|Wrap|Line Numbers
  1. <T>[] MyFunction(<T>[] Inputs, bool bOrder)
Is this possible in C# and what problems can I expect while using it?
Feb 28 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 190
It should work fine. For an individual method, you would declare:
Expand|Select|Wrap|Line Numbers
  1. T[] MyFunction<T>(T[] Inputs, bool bOrder){...}
or for a class you would declare:
Expand|Select|Wrap|Line Numbers
  1. class MyClass<T>{
  2. T[] MyFunction(T[] Inputs, bool bOrder){..}
  3. }
Feb 28 '09 #2

Expert 100+
P: 190
Also, look into the Array.Sort<T>(T[], IComparer<T>) if all you want to do is sort a generic array with a specified direction.
Feb 28 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.