IEnumerable Question

G

george r smith

All,

I have written the following code and it works but I am having a hard time
understanding the following call:
public static void PrintBitArray(IEnumerable aBitArray, int myWidth )

Here I am passing in a BitArray with a type of IEnumerable. Now I know that
IEnumerable is an interface and that BitArray implements the IEnumerable
interface. What I do not understand is why you use "(IEnumerable aBitArray"
in the call.

thanks
grs





====
in Main()
BitArray aBitArray = new BitArray(64);

PrintBitArray(aBitArray, 8);
.....

=====
public static void PrintBitArray(IEnumerable aBitArray, int myWidth )
{
IEnumerator myEnumerator = aBitArray.GetEnumerator();
int i = myWidth;
while (myEnumerator.MoveNext())
{
if ( i <= 0 )
{
i = myWidth;
Console.WriteLine();
}
i--;
Console.Write("\t{0}", myEnumerator.Current);
}
Console.WriteLine();
}
 
G

Guest

Hi George,

Thank you for posting in the community!

Based on my understanding, you have some concept questions on passing
parameter to method with interface.

==============================
In C#, when passing an class instance to a method parameter, it will do
implicit reference cast for you. If the cast is not possible, a comiple
error will generate.

The implicit reference conversions are:

1. From any reference-type to object.
2. From any class-type S to any class-type T, provided S is derived from T.
3. From any class-type S to any interface-type T, provided S implements T.
4. From any interface-type S to any interface-type T, provided S is derived
from T.
5. From an array-type S with an element type SE to an array-type T with an
element type TE, provided all of the following are true:
5.1. S and T differ only in element type. In other words, S and T have
the same number of dimensions.
5.2. Both SE and TE are reference-types.
6. An implicit reference conversion exists from SE to TE.
7. From any array-type to System.Array.
8. From any delegate-type to System.Delegate.
9. From the null type to any reference-type.

Your condition meet the implicit reference conversions 3, so the parameter
passing will succeed.

For more information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_6_1_4.asp

==============================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi George,

Thanks very much for your feedback.

I am glad my reply makes sense to you. If you have any further concern,
please feel free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top