vimwiki/tech/interface.wiki

74 lines
2.0 KiB
Plaintext

= Interface =
An interface is a paradigm where an abstract class defines some set of abstract
functions that a class inheriting it can define. This is a core concept in C#.
In C# an interface can be declared with the `interface` keyword in its
declaration. However, there are several built in interfaces that are very
useful.
== Built in C# interfaces ==
=== System.IComparable ===
Something that wants to be IComparable must impliment
`public int CompareTo(object obj)`. This must return,
* Less than zero if the current instance preceds the object specified in sort
order
* Zero if the current instance is in the same position in the sort order as the
given object
* Greater than zero if the current instance follows the object given
All numeric types already have this, as do String Char and DateTime
=== System.IDisposable ===
Something that is IDisposable can be freed, it is very useful when working with
unmanaged resources, like database connections or filestreams. To impliment
this, a class must impliment `public void Dispose()`. This Dispose function
will be called if the object is used in a `using()` block statement for
example.
=== System.IEnumerable ===
Something that is IEnumerable can be used with an enumerator object. This
allows some object to work with LINQ queries. IEnumerable also requires
`IEnumerator<T>` to be defined. `IEnumerator<T>` also needs
[[#System.IDisposable]] to be implimented.
{{{
public class FooEnumerable : IEnumerable<string>
{
private string _data;
//must impliment
public IEnumerator<string> GetEnumerator()
{
return new FooEnumerator(_data);
}
//must impliment
private IEnumerator GetEnumerator1()
{
return this.GetEnumerator();
}
//now override the IEnumerator one
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator1();
}
}
public class FooEnumerator : IEnumerator<string>
{
//required
public string Current
{
get
{
return "some val";
}
}
}
}}}