Showing posts with label iterator. Show all posts
Showing posts with label iterator. Show all posts

Friday, May 2, 2008

Sorting with help of delegate Comparison.

    When we use Generic class List(T), we can easy to sort it with help of method Sort and delegate Comparison.

Sample.

Definition of class Data:

internal class Data
{
internal int id;
internal string name;
internal Data(int id, string name)
{
this.id = id;
this.name = name;
}
}



Initialization of List<Data>:



List<Data> dataList = new List<Data>();
dataList.Add(new Data(4, "London"));
dataList.Add(new Data(2, "Rome"));
dataList.Add(new Data(3, "New York"));
dataList.Add(new Data(5, "Paris"));
dataList.Add(new Data(1, "Jerusalem"));

DataServices.PrintList("by original order:", dataList);



Sorting dataList by field id:



dataList.Sort(delegate(Data data1, Data data2) { return data1.id - data2.id; });
DataServices.PrintList("by id:", dataList);



Sorting dataList by field name:



dataList.Sort(delegate(Data data1, Data data2) { return String.Compare(data1.name, data2.name); });
DataServices.PrintList("by name:", dataList);



Console output:



consoleComparison



Reference:



List(T).Sort Method



Comparison(T) Generic Delegate



Using IComparable and IComparer to compare objects

Tuesday, April 29, 2008

ReferencesManager 1.0.2 : Utility to check .NET assembly references.

    New build of ReferencesManager (v1.0.2) is released.

    The build include new command line switch:

-path=<customer path>

which allows to define additional folders to search referenced assemblies. For example:

ReferencesManager.exe Main.dll -path=D:\0;D:\1

Downloading ReferencesManager 1.0.2

Sources can be downloaded from "C# tips samples" project.

References:

Utility to check .NET assembly references

Wednesday, April 23, 2008

CSharp (c#) method String.Join().

When we should create a string, which consists of array of strings with a separator usually we use a iterator (for, foreach).

For example,

string[] stringData = new string[] {"one", "two", "three"};
string outputContent1 = "";
foreach (string item in stringData)
{
outputContent1 += item + ";";
}
outputContent1 = outputContent1.Remove(outputContent1.Length - 1);
Debug.Print("output #1: {0}", outputContent1);



Now, we can use csharp (c#) method String.Join:



string outputContent2 = String.Join(";", stringData);
Debug.Print("output #2: {0}", outputContent2);



Output window:



joinOutput



Reference:



String.Join Method

Tuesday, April 15, 2008

Utility to check .NET assembly references

Sometimes, when a complex project (many source solutions, dlls, references) is built and run, then we receive a problem with references (here we don't speak about signed assemblies). For example, all dlls has a reference to basic .dll, but a specific .dll has a reference to obsolete version of the basic .dll.

Sample:

MainUtils has a reference to Utils1 (v2.0.0.0) and Utils2 (v2.0.0.0), but Utils2 (v2.0.0.0) has a reference to obsolete version of Utils1 (v1.0.0.0). See folders TestData4References in solution csharp-samples.

referenceDiagram

When a MainUtils will be run, then a error can be shown.

Utility ReferencesManager.exe helps to show all references and output list of all assemblies full names. This information helps to see same assemblies with different versions.

To start the utility it is necessary to type name of initial executable (or .dll) file:

ReferencesManager.exe MainUtils.exe > MainUtils.log

Content of MainUtils.log:

mainUtilsLog

Received log allows to find Utils1 with different versions 1.0.0.0 and 2.0.0.0 and Utils2, which references to Utils1 version 1.0.0.0.

Limitations of version 1.0.1:

  • The utility finds assemblies in GAC and in folder of initial assembly only.
  • The utility doesn't find referenced (in c# source project), but actually not used assemblies.
  • The utility doesn't output list of problematic assemblies.

Downloading ReferencesManager.exe.

References:

Reflector for .NET

Create and Share Flow Charts, Diagrams and More

Monday, April 7, 2008

Loops and iterators in c# (5 ways).

It is suggested to inspect several ways to implement loop in c#.

For example, we need display all days: Sunday .. Saturday with help of loop (or iterators).

Operator For:

static void sample_For_Days()
{
for (int index = 0; index < DaysNames.DAYS.Length; index++)
{
string day = DaysNames.DAYS[index];
Console.Write(day + " ");
}
Console.WriteLine();
}




Operator foreach:



foreach (string day in DaysNames.DAYS)
{
Console.Write(day + " ");
}
Console.WriteLine();





Method Array.Foreach:



Array.ForEach(DaysNames.DAYS, 
delegate(string day) { Console.Write(day + " "); });
Console.WriteLine();


Operator ForEach with help of interfaces IEnumerable and IEnumerator


class implementation



public class DaysOfTheWeek_IEnumerator : IEnumerable, IEnumerator
{
int index;
public DaysOfTheWeek_IEnumerator()
{
Reset();
}
#region IEnumerable
public IEnumerator GetEnumerator()
{
return this;
}
#endregion

#region
IEnumerator
public bool MoveNext()
{
return (++index < 7);
}
public void Reset()
{
index = -1;
}
public object Current
{
get { return DaysNames.DAYS[index]; }
}
#endregion
}





usage of foreach



DaysOfTheWeek_IEnumerator week = new DaysOfTheWeek_IEnumerator();

foreach (string day in week)
{
Console.Write(day + " ");
}
Console.WriteLine();





Operator ForEach with help of interfaces IEnumerable and statement yield:



class implementation:



public class DaysOfTheWeek_yield : IEnumerable
{
#region IEnumerable
public IEnumerator GetEnumerator()
{
for (int i = 0; i < DaysNames.DAYS.Length; i++)
{
yield return DaysNames.DAYS[i];
}
}
#endregion
}





usage of foreach:



DaysOfTheWeek_IEnumerator week = new DaysOfTheWeek_IEnumerator();

foreach (string day in week)
{
Console.Write(day + " ");
}
Console.WriteLine();





Console output shows, all methods display same results:



greenshot_2008-04-07_10-41-49



Reference:



foreach, in (C# Reference)



Iterators (C# Programming Guide)



yeild (C# Reference)