Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Friday, 23 December 2011

C# Modulo Operator - Find easy the remainder of a division.






Percentage symbol



You want to see examples of the modulo division operation in the C# language, which allows you to execute code once every several iterations of a loop. The modulo operator uses the percentage sign % character in the lexical syntax and has some unique properties. Here we look at the modulo division operation.
Estimated costs of instructions

Add:         1 ns
Subtract:    1 ns
Multiply:  2.7 ns
Divide:   35.9 ns

Example 1

Wednesday, 30 November 2011

c# how to dispose objects of Dictionary

 
problem

c# dispose objects of Dictionary
you cannot dispose the object of a Dictionary with "foreach" process, you can do with "while" getting the Enumerator of the Dictionary.
difficulty level

5/10 :|
compatibility

c#
solution

In the follow example, I scan serially a Dictionary generic of <string, IInDataObject> type and I dispose its hosted objects.
Each object in the dictionary, each Value is IInDataObject interface. This interface implements IDisposable so can call Dispose();

//get the Enumerator in order to scan with while (and not with foreach)
System.Collections.IEnumerator enumerator = dataObjectList.GetEnumerator();
//perform the Enumerator in while loop
while (enumerator.MoveNext()) {
    //get the pair of Dictionary
    KeyValuePair<string, IInDataObject> pair =
        ((KeyValuePair<string, IInDataObject>)(enumerator.Current)); 
    //dispose it
    pair.Value.Dispose();
       
}

Tuesday, 29 November 2011

csharp, process the items of Dictionary serial and faster

The Dictionary type abstracts out its looping logic into enumerators: these are accessed through the foreach loop and the GetEnumerator method. In this article, we demonstrate the GetEnumerator method, which exhibits better performance than the foreach loop.

Examples

Let's examine the most common and easiest way to loop through a Dictionary instance. The foreach loop here actually compiles into intermediate language that uses the GetEnumerator method, MoveNext, and Current, as well as a try/finally block.
Finally
Looping over Dictionary with foreach [C#]

static int A(Dictionary<string, int> d)
{
    int a = 0;
    foreach (var pair in d)
    {
 a += pair.Value;
    }
    return a;
}
Next, this method demonstrates the GetEnumerator and MoveNext methods and the Current property directly. This code is compiled to the same intermediate language except the try/finally block is absent.
Looping over Dictionary with GetEnumerator [C#]

static int B(Dictionary<string, int> d)
{
    int b = 0;
    var enumerator = d.GetEnumerator();
    while (enumerator.MoveNext())
    {
 var pair = enumerator.Current;
 b += pair.Value;
    }
    return b;
}

Monday, 28 November 2011

c# - how to move a project to different file location

- Get backup of the Directory with Project (you don't know what Microsoft can do for you)
- From Visual Studio remove the project for Solution
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Move the directory of the Project to the new directory location you desire.
- Remove all *.*scc files from all directories of the project. Some of them might be hidden and read only, so pay attention.
- Right click on Solution and click on Add -> Existing project
- Navigate the dialog and open the .csproj file you placed in new file location
- If you use the Safe Source you might get the misleading message "The associated source control plug-in is not installed or vould not me initialized". Select "Temporarily work uncontrolled" an click ok.
- If you use Source safe do Check in the pendings (ctrl-wg). This step is important if you use Source safe!
- Rebuild your solution and your will get error because this Project is not referenced any more to other Project that was before we removed it. So add this Project in References of the other project where use it.