If it walks like a duck and quacks
(If something walks like a duck and quacks like a duck, then it's a duck)
Duck object as a parameter and calls the object's Quack and Walk methods. In the same utipizirovannom (and, as a result, in dynamically typed) language, a similar function can take as a parameter some abstract object and try to call the same methods in it (perhaps, checking their presence). In a statically typed language, this behavior can be achieved using inheritance and interface implementations, but this will be an expression of the is-a form, unlike has-a in dynamic languages.foreach operator. Most textbooks say that to support foreach semantics in a class, the System.Collections.IEnumerable interface must be implemented (explicitly or implicitly). In fact, this is not a completely valid statement - if we talk about C #, then the mentioned interface is completely optional to implement.It is a collection of the type of the iteration variable. If expression has the value of anull, aSystem.NullReferenceExceptionis thrown.
It can be used to make it out of the following criteria:
Ccontains a public instance method for the signature of the following text.Econtains a public instance method with the signatureMoveNext()and the return typebool.- The current instance of the current value. This is the type of the collection type.
using System;
using System.Collections. Generic ;
namespace DuckTyping
{
class Collection
{
public Enumerator GetEnumerator()
{
return new Enumerator();
}
}
class Enumerator
{
private bool moveNext = true ;
public object Current
{
get { return "Enumerator.Current" ; }
}
public bool MoveNext()
{
if (!moveNext)
return false ;
moveNext = false ;
return true ;
}
}
class Program
{
static void Main()
{
foreach ( object o in new Collection())
Console .WriteLine(o);
}
}
}
* This source code was highlighted with Source Code Highlighter .var digits = new List < int > { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
* This source code was highlighted with Source Code Highlighter .System.Collections.ICollection interface prior to .NET 2.0 was a completely stupid creation. In 2.0, it was repaired: System.Collections.Generic.ICollection<T> allows you to add and remove elements, traverse the foreach mentioned above and find out the number of elements in the collection.ICollection<T> will be called a collection. But it was not there - practically no one implements it. Of the thousands of classes. NET BCL, only a few dozen can boast that they have a publicly accessible constructor without parameters and can be brought to ICollection<T> .Add() method, if it exists at all. Now the requirements for the collection from the point of view of the Collection Initializer are the following: the implementation of IEnumerable and the presence of at least one public method Add() .Add() methods”. The argument list can thus be heterogeneous. In other words, having such a class:public class Plurals : IDictionary< string , string >
{
public void Add( string singular, string plural);
public void Add( string singular);
public void Add(KeyValuePair< string , string > pair);
}
* This source code was highlighted with Source Code Highlighter .var myPlurals = new Plurals{ “collection”, { “query”, “queries” }, new KeyValuePair(“child”, “children”) };
* This source code was highlighted with Source Code Highlighter .Source: https://habr.com/ru/post/41377/
All Articles