Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
Generics and assigning variables
Carl
Jun 14 2007 2:03 PM
1.8k
Reply
I try to define a generic function in c# that is capable of manipulating an array of integers, or an array of bools, depending on the parameter given to the function.
Is this possible?
E.g. I have the following code, this will result in a compile error.
void
fill_array<T>(
int
count,
ref
T[] array_out)
{
for
(int i= 0; ii < count; count++) {
if
(
typeof
(T) ==
typeof
(
int
)) array_out[i] = i % 2;
else
array_out[i] = (i % 2) == 1;
// results in cannot cast int to T, and cannot cast bool to T.
}
return
;
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Problem while writing to Remote Location in a .Net Thread
Events and sender object