miércoles, 27 de noviembre de 2013

Delegates and Lambda Expressions... fearless

Intended for: .NET medium-skilled developers

Delegates


Before we start this entry, we have to warn you about this is not a book so, we don't explain with details this theme. Its frequent to watch developers with fear to this concepts because are hard to understand even with a book in your hands. I've try to explain this in a simple way.


A deletate is a function definition to be used as a parameter to other one. Is as easy as the previous phrase explains it. Maybe you can find better explained definitions about delegates; but they could be more confusing than this.

public delegate bool DelegateFunction(int parameter1, int parameter2);

This is the way to declare a delegate, is enough. It is usefull as a "shell" to be passed as parameter another function with this kind of declaration: two integers as entry and a boolean as returned type.

Now I'm going to create a function that uses the delegate as parameter:

public string ExampleFunctionWithParameterDelegate(DelegateFunction df, int p1, int p2)
{
if (df(p1, p2))
{
return String.Format("Function condition accomplished df using {0} and {1}",
                    p1,
                    p2);
}
return String.Format("Function condition NOT accomplished df using {0} and {1}",
                    p1,
                    p2);
}

This drives us to understand how delegates works. Now we have to create functions that have the entry and return parameters as follows:

public bool DelegateExampleFunctionP1IsLessThanP2(int parameter1, int parameter2)
{
return parameter1 < parameter2;
}

public bool DelegateExampleFunctionP1IsMoreThanP2(int parameter1, int parameter2)
{
return parameter1 > parameter2;
}

Now, we have to connect everything with the following calls:

var p1 = 5;
var p2 = 2;
var returned1 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsLessThanP2, p1, p2);
var returned2 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsMoreThanP2, p1, p2);


Can you imagine what are the values for the variables returned1 and returned2 after the execution of the instructions? These values are the following ones:

returned1 = "Function condition NOT accomplished df using 5 and 2"
returned2 = "Function condition accomplished df using 5 and 2"

As we can see, this allows us to make flexible functions, even "mutants". Now I'm going to explain Lambda expressions, using the same example as delegates.

Lambda Expressions


Following the previous idea here is an example of Lambda expression that is usefull  to know how they work, making comparitions with the previous develop I've mark in yellow Lambda expressions:


var returned1 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsLessThanP2, p1, p2);
var lambdaReturned1 = ExampleFunctionWithParameterDelegate(
(parameter1, parameter2) => { return parameter1 < parameter2; }, 
         p1, p2);

var returned2 = ExampleFunctionWithParameterDelegate(
DelegateExampleFunctionP1IsMoreThanP2, p1, p2);
var lambdaReturned2 = ExampleFunctionWithParameterDelegate(
(parameter1, parameter2) => { return parameter1 > parameter2; },
p1, p2);


We observe that the difference between the previous delegates code and now is that now we don't need the auxiliary created functions, I've made it "on the fly" and without direct define inside the call parameters of the call of main function with the delegate.

As you can imagine, the following is the result that is the same than the previous one:

returned1 = "Function condition NOT accomplished df using 5 and 2"
lambdaReturned1 = "Function condition NOT accomplished df using 5 and 2"
returned2 = "Function condition accomplished df using 5 and 2"
lambdaReturned2 = "Function condition accomplished df using 5 and 2"

Lambda expressions and delegates are implemented in several functionallities inside the framework elements. One of the most frequent places to be founded is inside the objects of System.Collections.Generic class, for example inside the lists and it will be usefull to be used in the querys over that collections.
C# and Developing

jueves, 14 de noviembre de 2013

Secure File Deletion under Windows - File Predator



Intended for: all kind of users


I don't want to scare you but, Windows file deletion (and its recycle bin deletion) IS NOT SAFE. In fact, it never have been secure, so you have to take care with your files when you use a classic file deletion your files never be really deleted until that files where completely overwritten.

Before starting this entry, I will explain how Windows delete the files over hard disk, in order to understand why is so important to delete files in a correct manner, more important if you send to trash the hard disk itself. I have to explain that I'm using my memory to recover the Operating System's of my carreer so the explanation couln't be a hundred percent accurate; my idea is to explain to anyone without a big set of details. I accept any comment to improve this post.

Cómo se distribuyen los ficheros a lo largo del disco duiro
In a Windows OS (same than Linux) files are stored in the hard disk in a sequential mode (this is not exactly, but approximately). Everything are bit sequences (zeros and ones). Part of this information is the i-node that
is readed by operating system to identify the basic information of the file: the memory position where the file starts and their metadata as the file lenght.
Dos ficheros eliminado en el anterior esquema del disco duro
The only thing that Windows do is to remove the i-node  in both steps (send to bin and cleanup it) and the files are kept in the hard disk until Windows overwrites the data with new one. This can happen in a short time or... never!!!
Cómo las aplicaciones recuperan los datos

There are lots of simply and cheap tools (even free) that are capable to recover these data after formatting the hard disk (always if is not a low level format), beacuse of that I repeat: Windows data deletion IS UNSAFE. That tools read the hard disk and test which files are i-node orphans and recreate a new one for them in order to Windows shows them normally. If that files aren't overwrited partially or totally, restore applications make the file recover. About these recovery tools we talk in the following post: How to recover lost files; but now we are looking at the safe file deletion.

Presentación File Predator
To help us to cleanup our files in a safe way, I have take the chance of create an application by myself.There are more and different, and not free. Mine is free an you can download it here:


Before start you have to be agree with these points and the agreement inside the application files:

  • This application is totally free and you can't pay or sell it. This is a non commertial application.
  • You can put it in any website, but remember that you have to link to this blog and the entry post. You can't modify the content inside the rar file inside my web and the license file inside them.
  • Use it carefully! If you erase something with this application you'll never can recover them!
  • You're using this UNDER YOUR OWN RESPONSIBILITY.
  • I don't have any responsability on any trouble that you can have: not viruses inside the exe files, not errors of the application itself and not human errors during the application management.
If you're not agree with the previous, don't download it and don't use it! I'm completely clear: is not my problem. This is a present for you, it costs to me effort and work and I bring to you "AS IS". Of course I will always trying to make a free-bug application but, as everyone knows, there are lots of bugs that make big companies loose lots of money; so obviouslly a free application could contain it (sure it have). If you detect fails and want to report them, please comment at the end of this post.

The use of application is very simple and intuitive, I left it to you in order to you discover it and is further posts I'll explain you how to do it. I left the compilation to be uncompressed (in rar format) and in next posts I'll explain you how to use it, .NET framework 4.0 is requiered.

In next entries I'll explain how to make your own secure file deletion algorithms to add to your application. Don't lose this opportunity!


Windows

 

Friendly Websites:

  • Manuel Enrique Díaz Rodríguez
  • CuRadio
  • Copyright © DotNet Pathways 2015
    Distributed By My Blogger Themes | Designed By Templateism