Friday, April 11, 2008

Mainstream usage of yield (c#).

Let's review when csharp statement yield can be really useful. When we need process list (for example, display or calculate a parameter) of generated data we can use simple way and create array:

static public int[] PowerByArray(int number, int exponent)
{
int[] results = new int[exponent];
int result = 1;
for (int i = 0; i < exponent; i++)
{
result *= number;
results[i] = result;
}
return results;
}
call method PowerByArray:
Trace.Write("powers list (by array): ");
int[] powers = PowerCalculator.PowerByArray(2, 10);
foreach (int value in powers)
{
Trace.Write(string.Format("{0} ", value));
}
Trace.WriteLine("");
and next result will be shown:

power_by_array

The way is clear, but it requests to create array. Frequently we cannot allow to create a array, because number of elements and/or size of needed memory.

Usage of yield helps us to avoid creation of array:

static public IEnumerable Power(int number, int exponent)
{
int result = 1;
for (int i = 0; i < exponent; i++)
{
yield return result *= number;
}
}

Calling method Power

Trace.Write("powers list (by array): ");
int[] powers = PowerCalculator.PowerByArray(2, 10);
foreach (int value in powers)
{
Trace.Write(string.Format("{0} ", value));
}
Trace.WriteLine("");

generates next output:

power_by_yield

Reference:

yield (C# Reference)

simple example of yield (reverse)

Loops and iterators in c# (5 ways)

Statement "yield".

    When we want to use operator foreach, but want to hide details of implementation we can use interface IEnumerable.

     Review next sample:

Let's display list of cities:

Cities cities = new Cities();
foreach (string city in cities)
{
Trace.WriteLine("city: " + city);
}



where class Cities is implemented:



public class Cities
{
readonly string[] cities = { "London", "Paris", "Rome", "Jerusalem" } ;
public IEnumerator GetEnumerator()
{
return cities.GetEnumerator();
}
}



normalOrder



Seems enough simple.


Now we need display cities in reverse order



Trace.WriteLine("Reverse order: ");
foreach (string city in cities.Reverese)
{
Trace.WriteLine("city: " + city);
}



reverseOrder



To receive reverse order we can add property Reverse:



public IEnumerable<string> Reverese
{
get
{
for (int i = cities.Length - 1; i >= 0; i--)
{
yield return cities[i];
}
}
}





Reference:



yield (C# Reference)



Create Elegant Code with Anonymous Methods, Iterators and Partial Classes.

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)

Sunday, April 6, 2008

How to listen file change notification.

        When we want to receive a notifications about file system changes, we can use class FileSystemWatcher.

Code sample:

string testFolder = Path.GetTempPath();
string testFilePath = Path.Combine(testFolder, "test.tst");
File.Delete(testFilePath);

using (FileSystemWatcher watcher = new FileSystemWatcher(testFolder, "*.tst"))
{
watcher.NotifyFilter =
NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName |
NotifyFilters.CreationTime | NotifyFilters.Attributes |
NotifyFilters.Size | NotifyFilters.Security;
watcher.Created += OnFileChanged;
watcher.Changed += OnFileChanged;
watcher.Deleted += OnFileChanged;
watcher.EnableRaisingEvents = true;

File.WriteAllText(testFilePath, "text");
File.AppendAllText(testFilePath, "additional text");
File.Delete(testFilePath);
}




Output window content:



FileSystemWatcherOutputContent



Notes:



Unfortunately, my experience shows, that FilesystemWatcher doesn't work absolutely correct. In many cases, it doesn't catch a file changing.



Reference:



FileSystemWatcher Class

Thursday, April 3, 2008

How to create unique temporary file name and folder.

        As we investigated, it is possible to get temporary Windows folder by method Path.GetTempPath and create temporary file by method Path.GetTempFileName().

Unfortunately, these methods cannot help us to create unique temporary folder or temporary file (method Path.GetTempFileName() can generate only 65 536 unique file names at the same time).

It is suggested to generate unique path and name to use a globally unique identifier (GUID) with help of method Guid.NewGuid(). "Such an identifier has a very low probability of being duplicated." by MSDN.

Method, which can generate unique temporary folder:

static string GetUniquePath()
{
string uniquePath;
do
{
Guid guid = Guid.NewGuid();
string uniqueSubFolderName = guid.ToString();
uniquePath = Path.GetTempPath() + uniqueSubFolderName;
} while (Directory.Exists(uniquePath));
Directory.CreateDirectory(uniquePath);
return uniquePath;
}


The method generated and created such paths:



C:\Documents and Settings\name-f\Local Settings\Temp\b0dc9b66-a32c-586c-815b-dfed844df58c



C:\Documents and Settings\name-f\Local Settings\Temp\18a09210-9107-4a19-ab11-a246c36fec54



By the same way, can be generated and created temporary file path.



Important notes:



1. Don't forget to delete created temporary folder and files after usage.



2. Published method GetUniquePath is not protected for multi-threaded usage.



Reference:



Guid Structure



Guid.NewGuid Method

How to redirect Trace output.

As we know, for tracing we can use method Trace.WriteLine(). But usually, we want to avoid a creating of detailed log file in production mode and to enable the tracing for testing and debugging purposes only by a configuration changing. So, we should find a way to redirect output of Trace.WriteLine() from standard output to a log file and configures the redirection without rebuilding of application.

Let's write next c# code:

Trace.WriteLine("messageTrace");
Trace.WriteLine("messageError", "Error");
Trace.TraceError("traceErrorMessage");

and define .config file:

redirectTrace_va_ConfigFile

Thus, we delete "Default" Trace Listener and add our "myListener' of type TextWriterTraceListener with parameter (log file name) c:\myListener.log.

Additionally, attribute "autoflush" is set in True and method Trace.WriteLine() will store input messages to log file immediately.

Content of file myListener.log:

myListenerLogFileContent

Reference:

How to configure Trace listener

Tuesday, April 1, 2008

How to receive temporary path and file name (c#).

    To receive standard Windows temporary path we can use c# method Path.GetTempPath().

To receive uniquely temporary file we can use method Path.GetTempFileName(). The method creates a zero-byte in temporary folder and returns a full path of that file. Path of the file is created by format: <temporary folder>\tmp<4 hex symbols>.tmp (for example, <temporary folder>\tmpF1FB.tmp).

   Let's investigate how a method GetTempFileName creates a uniquely temp file:

string tempFolder = Path.GetTempPath();
string uniqueTempFileName = Path.GetTempFileName();
string tempFolder_via_Environment = Environment.GetEnvironmentVariable("TEMP");

Debug.WriteLine("tempFolder: " + tempFolder);
Debug.WriteLine("tempFolder_via_Environment: " + tempFolder_via_Environment);
Debug.WriteLine("uniqTempFileName: " + uniqueTempFileName);

for (int i = 0; i < 0xFFFF; i++)
{
uniqueTempFileName = Path.GetTempFileName();
Debug.WriteLine("uniqTempFileName: " + uniqueTempFileName);
Debug.WriteLine(String.Format("File {0} exists: {1}", uniqueTempFileName, File.Exists(uniqueTempFileName)));
}
Output Window:



output1



So, we can see rule, how GetTempFileName builds file name: "tmp" + counter + ".tmp". If a  file with next temporary file name exists, then GetTempFileName will generate next temporary file name.



File with name tmp3DBA.tmp was created manually and see result (tmp3D9, tmp3DBB) in Output Window



output2



When all temporary names will be created, that exception IOException will be raised:



exceptionQuickWatch



Reference:



Path.GetTempPath Method



Path.GetTempFileName Method