In framework 1.0:
//1. declare the delegate signature - takes two int, and returns int
delegate int Add(int firstNumber, int secondNumber);
//2.create a function matches with the above delegate signature
private int AddNumbers(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
//3. instantiate the delegate
Add add = new Add(this.AddNumbers);
//4. call the delegate to get the result
int result = add(Convert.ToInt32(this.txtFirst.Text), Convert.ToInt32(this.txtSecond.Text));
In framework 2.0:
Now you can have anonymous method.
//1. declare the delegate signature - takes two int, and returns int
delegate int Add(int firstNumber, int secondNumber);
//2. now you can instantiate the delegate with the anonymous method body inside of the brackets. 2. and 3. steps in framework 1.0 are now merged into one step as below
Add add = delegate(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
};
//3.same here to call the delegate to get the result
int result = add(Convert.ToInt32(this.txtFirst.Text), Convert.ToInt32(this.txtSecond.Text));
In framework 3.5:
First, you can instantiate the delegate with lambda expressions
//1. declare the delegate signature - takes two int, and returns int
delegate int Add(int firstNumber, int secondNumber);
//2. now you can instantiate the delegate with lambda expressions. This below step does the exact same thing as the step 2 in framework 2.0.
Add add = (firstNumber, secondNumber) => firstNumber + secondNumber;
//3. same here to call the delegate to get the result
int result = add(Convert.ToInt32(this.txtFirst.Text), Convert.ToInt32(this.txtSecond.Text));
Second, you can also use the new Func delegate.
For example, we need the Func to have 2 input int and a return int as above. This below pre-defined one in .NET will suit us well:
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
//1. below line combines delegate delcaration, delegate instantiation, as well as the function body in lambda expression altogether.
Func<int, int, int> add = (firstNumber, secondNumber) => firstNumber + secondNumber;
//2. then to use same way to get the result.
int result = add(Convert.ToInt32(this.txtFirst.Text), Convert.ToInt32(this.txtSecond.Text));
Get it? It's now reducing from 4 lines of coding to 2 lines to achieve the exact same thing from Framework 1.0 to 3.5. Nice!
Thursday, October 21, 2010
Sunday, October 17, 2010
refreshing the memories - data structures
I. Dictionary vs. List:
C# Dictionary vs. List Lookup Time
Summary:
"Using Dictionary when the number of lookups greatly exceeds the number of insertions. It is fine to use List when you will have less than 4 items always. For lookups, Dictionary is usually a much better choice."
II. Dictionary vs. Hashtable
Why Dictionary is preferred over hashtable in C#?
"Because Dictionary is a generic class ( Dictionary ), so that accessing its content is type-safe (i.e. you do not need to cast from Object, as you do with a Hashtable)."
//dictionary
var customers = new Dictionary();
...
Customer customer = customers["Ali G"];
//hashtable
var customers = new Hashtable();
...
Customer customer = customers["Ali G"] as Customer; //requies boxing
III. SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
IDictionary Options - Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
"Generic Dictionary is the absolute winner for insert and search operations if used with generic dictionary interface or directly." :)
C# Dictionary vs. List Lookup Time
Summary:
"Using Dictionary when the number of lookups greatly exceeds the number of insertions. It is fine to use List when you will have less than 4 items always. For lookups, Dictionary is usually a much better choice."
II. Dictionary vs. Hashtable
Why Dictionary is preferred over hashtable in C#?
"Because Dictionary is a generic class ( Dictionary
//dictionary
var customers = new Dictionary
...
Customer customer = customers["Ali G"];
//hashtable
var customers = new Hashtable();
...
Customer customer = customers["Ali G"] as Customer; //requies boxing
III. SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
IDictionary Options - Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
"Generic Dictionary is the absolute winner for insert and search operations if used with generic dictionary interface or directly." :)
MySQL Engines: MyISAM vs. InnoDB
Reading a good article about - MySQL engines - MyISAM vs. InnoDB
Learned that InnoDB provides increased concurrency, enhanced performance and much more data integrity than MyISAM. If you need to put your statements in transaction, InnoDB engine is the right choice. MyISAM is known for its simplicity. It also uses less memory. The best part is that in one database, you can have "mix and match" table engines.
Syntax to swtich db engines:
ALTER TABLE tablename ENGINE = INNODB/MYISAM;
Learned that InnoDB provides increased concurrency, enhanced performance and much more data integrity than MyISAM. If you need to put your statements in transaction, InnoDB engine is the right choice. MyISAM is known for its simplicity. It also uses less memory. The best part is that in one database, you can have "mix and match" table engines.
Syntax to swtich db engines:
ALTER TABLE tablename ENGINE = INNODB/MYISAM;
Sunday, October 10, 2010
PHP - @mysql_query vs. mysql_query
Don't you ever wonder the difference between @mysql_query vs. mysql_query, like I do? What does the "@" symbol do in here? Here is what I find out:
If error reporting is set to *show*, the @ symbol before a function call will prevent the error from displaying.
However, for MySQL, it would show the syntax error only. To display MYSQL returned errors, you would need to use mysql_error() or mysql_errno() functions.
Little details like above is always helpful... :)
If error reporting is set to *show*, the @ symbol before a function call will prevent the error from displaying.
However, for MySQL, it would show the syntax error only. To display MYSQL returned errors, you would need to use mysql_error() or mysql_errno() functions.
Little details like above is always helpful... :)
Subscribe to:
Comments (Atom)