





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Notes on interface and collections
Typology: Papers
1 / 29
This page cannot be seen from the preview
Don't miss anything!
Defining Interfaces in C#
public struct SomeStruct : ISomeInterface, IPointy {...}
if (o is Class1) { Console.WriteLine("o is Class1"); a = (Class1)o; }
b = (Class2)o;
else
Console.WriteLine("o is neither Class1 nor Class2.");
Class2 c2 = new Class2();
Test(c2);
Console.ReadKey();
class Class
public class IsTest
public static void Main()
object [] myObjects = new object [6];
myObjects[1] = new Class2();
myObjects[3] = 32;
for ( int i = 0; i < myObjects.Length; ++i)
string s = myObjects[i] as string ;
if (s != null )
else
In the above example, each and every value is being cast to a string using the "as" operator and assigned to a string variable which is shown on the console. Invoking Interface Members at the Object Level
if(s[i] is IPointy) Console.WriteLine("-> Points: {0} ", ((IPointy)s[i]).Points); else Console.WriteLine("-> {0}'s not pointy!", s[i].PetName); } } The output follows in Figure 7-2. Interfaces As Parameters
Shape[] s = { new Hexagon(), new Circle(), new Triangle(), new Circle("JoJo")} ; for(int i = 0; i < s.Length; i++) { ... // Can I draw you in 3D? if(s[i] is IDraw3D) DrawIn3D((IDraw3D)s[i]); } } } Interfaces As Return Values
IDraw3D i3d = (IDraw3D)myLine; i3d.Draw(); Resolving Name Clashes
Understanding C# Iterator Methods