<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1467</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1467" rel="self"/>
  <title>petar.petrov.myopenid.com</title>
  <updated>Mon Jul 06 16:43:58 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor189684</id>
    <published>2009-07-06T16:43:58-07:00</published>
    <title>[C#] On IEnumerable&lt;&gt; comparision extension method</title>
    <content type="html">&lt;p&gt;Yes, you can't use this on IEnumerable&amp;lt;int&amp;gt; but the idea was to eliminate the against null on value types - it's always false ( value types cannot be null ). You can remove the zero counter and the checks in the loops. Doing so you will eliminate first.Count + second.Count checks and some increments/decrements. I think you can get the best version by splitting the code in two private methods for value and reference types equality check. Of course if you want you can stay with your current version it's quite good. &lt;/p&gt;

&lt;pre&gt;		public static bool ContentEquals&amp;lt;T&amp;gt;(
	this IEnumerable&amp;lt;T&amp;gt; first,
	IEnumerable&amp;lt;T&amp;gt; second,
	IEqualityComparer&amp;lt;T&amp;gt; comparer)
		{
			var isValueType = typeof(T).IsValueType;
			if (isValueType)
			{
				return ValueTypeContentEquals(comparer, first, second);
			}
			return ReferenceTypeContentEquals(comparer, first, second);
		}

		private static bool ReferenceTypeContentEquals&amp;lt;T&amp;gt;(IEqualityComparer&amp;lt;T&amp;gt; comparer, IEnumerable&amp;lt;T&amp;gt; first, IEnumerable&amp;lt;T&amp;gt; second)
		{
			throw new NotImplementedException();
		}

		private static bool ValueTypeContentEquals&amp;lt;T&amp;gt;(IEqualityComparer&amp;lt;T&amp;gt; comparer, IEnumerable&amp;lt;T&amp;gt; first, IEnumerable&amp;lt;T&amp;gt; second)
		{
			throw new NotImplementedException();
		}&lt;/pre&gt;</content>
    <author>
      <name>petar.petrov.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/928-ienumerable-comparision-extension-method/refactors/189684" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor174490</id>
    <published>2009-06-26T07:51:40-07:00</published>
    <title>[C#] On IEnumerable&lt;&gt; comparision extension method</title>
    <content type="html">&lt;p&gt;I have some suggestions about your code. 
&lt;br /&gt;The first thing to do is to constraint T to be a class, because (item == null) will be always false for value types.
&lt;br /&gt;After that you can optimize you code by counting the elements in the first sequence and in the second. If the second count becomes greater then the first one (you have two sequences with different length ) - they are different.
&lt;br /&gt;Place the cheep check (zero != 0) before iterating on itemCounts.
&lt;br /&gt;One last thing I think it will lead to a better performance is to use TryGetValue for the dictionary. Here's the code
&lt;br /&gt;:
&lt;br /&gt;if (itemCounts.ContainsKey(item))
&lt;br /&gt;{
&lt;br /&gt;	itemCounts[item]++;
&lt;br /&gt;}
&lt;br /&gt;else
&lt;br /&gt;{
&lt;br /&gt;	itemCounts[item] = 1;
&lt;br /&gt;}&lt;/p&gt;

&lt;p&gt;which is compiled as
&lt;br /&gt;if (itemCounts.ContainsKey(item))
&lt;br /&gt;{
&lt;br /&gt;	itemCounts[item] = itemCounts[item] + 1;
&lt;br /&gt;}
&lt;br /&gt;else
&lt;br /&gt;{
&lt;br /&gt;	itemCounts[item] = 1;
&lt;br /&gt;}
&lt;br /&gt;You always perform the check for the item and setting the value(it's a part of the algorithm). However itemCounts[item]++ (itemCounts[item] = itemCounts[item] + 1) is not that innocent. The indexer get will perform FindKey() (the same as ContainsKey) to get the value or throw an exception if the key is not found. In total it FindKey is called twice ! By changing the code to&lt;/p&gt;

&lt;p&gt;int count;
&lt;br /&gt;itemCounts.TryGetValue(item, out count);
&lt;br /&gt;itemCounts[item] = ++count;&lt;/p&gt;

&lt;p&gt;This hidden problem is eliminated.
&lt;br /&gt;Here's my proposition &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;		public static bool ContentEquals&amp;lt;T&amp;gt;(
	this IEnumerable&amp;lt;T&amp;gt; first,
	IEnumerable&amp;lt;T&amp;gt; second,
	IEqualityComparer&amp;lt;T&amp;gt; comparer) where T : class
		{
			// Declare a dictionary to count the occurence of the items in the collection			
			Dictionary&amp;lt;T, int&amp;gt; itemCounts = new Dictionary&amp;lt;T, int&amp;gt;(comparer);

			// null references are stored here.
			int zero = 0;
			var totalFirst = 0;

			// Increase the count for each occurence of the item in the first collection
			foreach (T item in first)
			{
				if (item == null)
				{
					zero++;
				}
				else
				{
					totalFirst++;
					int count;
					itemCounts.TryGetValue(item, out count);
					itemCounts[item] = ++count;
				}
			}

			var totalSecond = 0;
			// Decrease the count for each occurence of the item in the second collection
			foreach (T item in second)
			{
				if (item == null)
				{
					zero--;
				}
				else
				{
					totalSecond++;
					if (totalSecond &amp;gt; totalFirst)
					{
						return false;
					}
					int count;
					if (itemCounts.TryGetValue(item, out count))
					{
						itemCounts[item] = --count;
					}
					else
					{
						return false;
					}
				}
			}

			if (zero != 0)
			{
				return false;
			}

			// The count of each item should be 0 if the contents of the collections are equal
			foreach (int value in itemCounts.Values)
			{
				if (value != 0)
				{
					return false;
				}
			}

			// The collections are equal
			return true;
		}&lt;/pre&gt;</content>
    <author>
      <name>petar.petrov.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/928-ienumerable-comparision-extension-method/refactors/174490" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor155598</id>
    <published>2009-04-23T07:14:06-07:00</published>
    <title>[C#] On Plugin Loader</title>
    <content type="html">&lt;p&gt;Your code is just fine. Why do you want to use LINQ ? :)
&lt;br /&gt;Why not returning IEnumerable&amp;lt;Plugin&amp;gt; ?
&lt;br /&gt;However I have one thing to point out - not every DLL is a .NET assembly ;) You can add a try/catch but if you are absolutely sure that there's only .NET assemblies it's OK.&lt;/p&gt;

&lt;pre&gt;public static IEnumerable&amp;lt;Plugin&amp;gt; LoadPlugins()
{
	var pluginFolder = Path.Combine(Environment.CurrentDirectory, &amp;quot;plugins&amp;quot;);

	foreach (var pluginFile in Directory.GetFiles(pluginFolder, &amp;quot;*.dll&amp;quot;))
	{
		var pluginAssembly = Assembly.LoadFile(pluginFile);

		var plugins =
			from type in pluginAssembly.GetTypes()
			where Plugin.ImplementsInterface(type)
			select Plugin.CreatePlugIn(type);

		var plugin = plugins.FirstOrDefault();
		if (plugin != null)
		{
			yield return plugin;
		}
	}
}&lt;/pre&gt;</content>
    <author>
      <name>petar.petrov.myopenid.com</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/795-plugin-loader/refactors/155598" rel="alternate"/>
  </entry>
</feed>

