✅ understanding interfaces

An interface is supposed to provide extra functionality for a class. but the class has to manually implement the method specified in the interface then whats the point of having an interface if you're going to create the custom method anyway?
5 Replies
ero
ero2y ago
An interface is supposed to provide extra functionality for a class
this is an incorrect assessment interfaces provide contracts for other types to implement with them, an api can take the interface as an input and call methods on said interface instance, regardless of how it was implemented take System.Linq for example, which provides extension methods on IEnumerable<T>, an interface for enumerable collections many classes implement IEnumerable<T>; List<T>, arrays, Dictionary<TKey, TValue>, HashSet<T>, the list goes on. and you can even make your own classes that implement IEnumerable<T>. all of those are usable with Linq because they all provide their own implementation of the contract that IEnumerable<T> provides or take IParsable<T> as an example; it provides a contract where T must implement a way to be created from a string such an interface could be used in this (very naive, but it gets the point across) way:
public T ParseAs<T>(string value)
where T : IParsable<T>
{
return T.Parse(value, null);
}
public T ParseAs<T>(string value)
where T : IParsable<T>
{
return T.Parse(value, null);
}
you can see here that you can generically pass any T into ParseAs, as long as it implements IParsable<T> how this parsing happens is implemented in the concrete type of T itself, like this;
readonly struct Point : IParsable<Point>
{
public Point(int x, int y)
{
X = x;
Y = y;
}

public int X { get; }
public int Y { get; }

public static Point Parse(string s, IFormatProvider? provider)
{
string[] coordinates = s.Split(',');

int x = int.Parse(coordinates[0]);
int y = int.Parse(coordinates[1]);

return new(x, y);
}

public static bool TryParse(string? s, IFormatProvider? provider, out Point result)
{
// implementation
}
}
readonly struct Point : IParsable<Point>
{
public Point(int x, int y)
{
X = x;
Y = y;
}

public int X { get; }
public int Y { get; }

public static Point Parse(string s, IFormatProvider? provider)
{
string[] coordinates = s.Split(',');

int x = int.Parse(coordinates[0]);
int y = int.Parse(coordinates[1]);

return new(x, y);
}

public static bool TryParse(string? s, IFormatProvider? provider, out Point result)
{
// implementation
}
}
i can now use ParseAs<Point>("0, 0") from before without any issues, because Point does have its own custom implementation of the IParsable interface
TheBasedTaka
TheBasedTakaOP2y ago
So it's guarantees a standard set of rules for the class or object to have so you can guarantee some sort of basic functionality
ero
ero2y ago
that's the gist of it
TheBasedTaka
TheBasedTakaOP2y ago
Makes sense thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server