{"id":4632,"date":"2016-11-28T04:00:00","date_gmt":"2016-11-28T04:00:00","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=4632"},"modified":"2023-01-06T09:58:37","modified_gmt":"2023-01-06T09:58:37","slug":"assignment-examples-the-one-about-duck-typing-in-c","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/assignment-examples-the-one-about-duck-typing-in-c\/","title":{"rendered":"Assignment Examples: The One about Duck Typing in C#"},"content":{"rendered":"<style>\n.csharp {<br \/>color: #222222;font-family: Menlo, 'Courier New';font-size: small;margin: 0 5px;<br \/>}<br \/><\/style>\n<p>Induction is a process of logical reasoning, based on the transition from private to general provisions. The famous &#8220;duck test&#8221; is a prime example of such a process: If it walks like a duck and quacks like a duck, it must be a duck.<!--more--><\/p>\n<p>Applied to programming languages, and in particular to object-oriented languages, the &#8220;duck test&#8221; takes on a special meaning. In dynamically typed languages, the semantics of the use of the object is determined by the current set of its methods and properties. For example, in a statically typed language which doesn\u2019t support duck typing, it is possible to create the function with an object of the class \u201cDuck\u201d as a parameter with the call of \u201cQuack\u201d and \u201cWalk\u201d for methods of that object. In the duck typed language, the similar function can take some abstract object as a parameter and try to call the same methods. Something like duck typing is present in C#.<\/p>\n<p style=\"text-align: center;\"><span class=\"csharp\">Foreach<\/span><\/p>\n<p>Most books say that the interface <span class=\"csharp\">System.Collections.IEnumerable<\/span> should be implemented in the class to support the semantics of foreach. This is actually not true. If we talk about C #, it is not necessary to implement the mentioned interface.<\/p>\n<p>If you open the C # Language Specification and look in section 8.8.4, you will see the following:<\/p>\n<p>The type of the expression of a <span class=\"csharp\">foreach<\/span> statement must be a collection type (as defined below), and an explicit conversion (\u00a76.2) must exist from the element type of the collection to the type of the iteration variable. If the expression has the value <span class=\"csharp\">null<\/span>, a <span class=\"csharp\">System.NullReferenceException<\/span> is thrown.<\/p>\n<p>Type C is said to be a collection type if it implements the <span class=\"csharp\">System.Collections.IEnumerable<\/span> interface or implements the collection pattern by meeting all of the following criteria:<\/p>\n<p>C contains a public instance method with the signature <span class=\"csharp\">GetEnumerator()<\/span> that returns a struct-type, class-type, or interface-type, which is called E in the following text:<\/p>\n<p>E contains a public instance method with the signature <span class=\"csharp\">MoveNext()<\/span> and the return type <span class=\"csharp\">bool<\/span>.<\/p>\n<p>E contains a public instance property named Current that permits reading the current value. The type of this property is said to be the element type of the collection type (msdn.microsoft.com).<\/p>\n<p>So, the following code will compile and output the right message:<\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<\/p>\n<p>namespace DuckTyping<br \/>\n{<br \/>\n\tclass Collection<br \/>\n\t{<br \/>\n\t\tpublic Counter GetCounter()<br \/>\n\t\t{<br \/>\n\t\t\treturn new Counter();<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tclass Counter<br \/>\n\t{<br \/>\n\t\tprivate bool next = true;<\/p>\n<p>\t\tpublic object curr<br \/>\n\t\t{<br \/>\n\t\t\tget{ return &quot;Counter.curr&quot;; }<br \/>\n\t\t}<\/p>\n<p>\t\t\tpublic bool Next()<br \/>\n\t\t{<br \/>\n\t\t\tif (!next)<br \/>\n\t\t\t\treturn false;<\/p>\n<p>\t\t\tnext = false;<br \/>\n\t\t\treturn true;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tclass Program<br \/>\n\t{<br \/>\n\t\tstatic void Main()<br \/>\n\t\t{<br \/>\n\t\t\tforeach(object a in new Collection())<br \/>\n\t\t\t\tConsole.WriteLine(a);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p style=\"text-align: center;\">Collection Initializers<\/p>\n<p>This is a pretty pleasant innovation in C# 3.0:<\/p>\n<p>[code language=&#8221;csharp&#8221;]var\u00a0digits =\u00a0new\u00a0List&lt;int&gt; { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };[\/code]<\/p>\n<p>Now, we should determine the termine \u201cCollection\u201d. At this point, we can face some trouble. Before .NET 2.0, the interface <span class=\"csharp\">System.Collections.Icollection<\/span> was mostly useless. In 2.0, it was fixed. <span class=\"csharp\">System.Collections.Generic.ICollection&lt;T&gt;<\/span> allows you to add or delete elements, iterate through them using <span class=\"csharp\">foreach<\/span>, and get the amount of elements in the collection.<\/p>\n<p>The argument list may thus be heterogeneous. In other words, with such a class:<\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\npublic\u00a0class\u00a0Plurals : IDictionary&lt;string,string&gt;\u00a0<br \/>\n{<br \/>\n\u00a0\u00a0public\u00a0void\u00a0Add(string\u00a0singular,\u00a0string\u00a0plural);<br \/>\n\u00a0\u00a0public\u00a0void\u00a0Add(string\u00a0singular);\u00a0<br \/>\n\u00a0\u00a0public\u00a0void\u00a0Add(KeyValuePair&lt;string,string&gt; pair);\u00a0<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>This initializer will be legal:<\/p>\n<p>[code language=&#8221;csharp&#8221;]<br \/>\nvar\u00a0myPlurals =\u00a0new\u00a0Plurals{ \u201ccollection\u201d, { \u201cname\u201d, \u201c names\u201d },\u00a0new\u00a0KeyValuePair(\u201cwolf\u201d, \u201cwolves\u201d) };<br \/>\n[\/code]<\/p>\n<p>Overload resolution is performed separately for each element of the initialization list so all three methods will be used.<br \/>\nThanks for your attention!<\/p>\n<p style=\"text-align: center;\">References<\/p>\n<p>8.8.4 The foreach statement. (2011, February 23). Retrieved July 12, 2016, from https:\/\/msdn.microsoft.com\/en-us\/library\/aa664754(v=vs.71).aspx<\/p>\n<h2>Programming Assignments Help<\/h2>\n<blockquote><p><em>We hope you like our sample. Need help with other <a href=\"https:\/\/assignmentshark.com\/blog\/c-programming-code-examples-finding-a-palindrome\/\" target=\"_blank\" rel=\"noopener noreferrer\">C# programming code examples<\/a>? Are you looking for a site where you can get homework help? AssignmentShark.com has a team of dedicated academic experts who are knowledgeable in different disciplines. <\/em><\/p>\n<p><em>Remember that you can forget about your homework problem if you simply place an order on our site. Our experts are ready to assist you with any subject. If you are looking for someone to provide <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">online assignment help<\/a> for you, then you have found the right place. We are available 24\/7 for your convenience \u2013 you can contact us any time you want. Just go to our order form, fill in your requirements, ask to do <a href=\"https:\/\/assignmentshark.com\/do-my-programming-homework.html\" target=\"_blank\" rel=\"noopener\">my programming<\/a> task and set the deadline.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Induction is a process of logical reasoning, based on the transition from private to general provisions. The famous &#8220;duck test&#8221; is a prime example of such a process: If it walks like a duck and quacks like a duck, it must be a duck.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-4632","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=4632"}],"version-history":[{"count":24,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4632\/revisions"}],"predecessor-version":[{"id":13329,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4632\/revisions\/13329"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=4632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=4632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=4632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}