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

Monday, December 27, 2010

How to Generate Random String in C#, ASP.NET

To Generate a Random String is very Tricky and useful stuff to Know as we can use this functionality at the time of Generating Random Password automatically for each User Registering on the website.

I am sure this will help you a lot, here I am writing the Method to Generate the Random String.

private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++) { ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); builder.Append(ch); } if (lowerCase) return builder.ToString().ToLower(); return builder.ToString(); }

Now the above code will generate the Random String. If you want to have a Strong Password then password must contains Number. So lets go to generate Random Numbers:

public string GetRandomNumber()
{
StringBuilder strbuild = new StringBuilder();
strbuild.Append(RandomNumber(1000, 9999));
return strbuild.ToString();
}

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}

Now you are ready to generate a random password. for that you have to call these methods as given below,

public string GetPassword()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}

Wednesday, October 13, 2010

Delegates in C#

With different .NET frameworks coming to existence, the usage of delegates becomes more and more desirable.
To satisfy the needs .NET 3.5 introduced the whole new world of delegates.
Below I present a table with delegates’ names and descriptions which comes with .NET3.5 framework (.NET 4.0 has even more built-in delegates).
In order to be used a delegate first must be declared. During the declaration one defines a signature of the function that delegate may represent.
By having those predefined delegates we can skip the declaration part.
.NET offered not only a set of such convenient delegates but also it embedded Generic data type to many of them.
As you can see many delegates have Generic type as a parameter or/and return value.
It is very convenient. You can substitute the Generic type by any type in your code and the compiler will do the rest.


Delegate Description
Action Encapsulates a method that takes no parameters and does not return a value.
Action Encapsulates a method that takes a single parameter and does not return a value.
Action> Encapsulates a method that has two parameters and does not return a value.
Action Encapsulates a method that takes three parameters and does not return a value.
Action Encapsulates a method that has four parameters and does not return a value.
AppDomainInitializer Represents the callback method to invoke when the application domain is initialized.
AssemblyLoadEventHandler Represents the method that handles the AssemblyLoad event of an AppDomain.
AsyncCallback References a method to be called when a corresponding asynchronous operation completes.
Comparison Represents the method that compares two objects of the same type.
ConsoleCancelEventHandler Represents the method that will handle the CancelKeyPress event of a System.Console.
Converter Represents a method that converts an object from one type to another type.
CrossAppDomainDelegate Used by DoCallBack for cross-application domain calls.
EventHandler Represents the method that will handle an event that has no event data.
EventHandler Represents the method that will handle an event.
Func Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter.
Func Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.
Func Encapsulates a method that has two parameters and returns a value of the type specified by the TResult parameter.
Func Encapsulates a method that has three parameters and returns a value of the type specified by the TResult parameter.
Func Encapsulates a method that has four parameters and returns a value of the type specified by the TResult parameter.
Predicate Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.
ResolveEventHandler Represents the method that handles the TypeResolve, ResourceResolve, and Assembly Resolve events of an AppDomain.
UnhandledExceptionEventHandler Represents the method that will handle the event raised by an exception that is not handled by the application domain.


In this article I want to discuss different technique to deal with custom and pre-defined delegates.
There are many ways to skin a cat. I don’t like this saying, because I have two cats at home and I love them.
But there are many different ways how to use a delegate in your program for sure.
We can sort out the delegates from table.

1. Generic type functions and methods
* Action
* Func
* Converter
* Comparison
* Predicate
2. Event handlers
* AssemblyLoadEventHandler
* ConsoleCancelEventHandler
* EventHandler
* EventHandler
* ResolveEventHandler
* UnhandledExceptionEventHandler
3. Others
* AppDomainInitializer
* AsyncCallback (we are already familiar with this delegate – Part2)
* CrossAppDomainDelegate

My intention is mostly talk about the first three delegates Action, Func, Converter.
I want to demonstrate how to use these delegates in different ways.
Eventually the compiler would generate pretty similar code, but from programming stand point this technique presents great difference.