Ada Lovelace

Augusta Ada King, Countess of Lovelace (10 December 1815 – 27 November 1852), born Augusta Ada Byron and now commonly known as Ada Lovelace, was an English mathematician and writer chiefly known for her work on Charles Babbage's early mechanical general-purpose computer, the analytical engine. Her notes on the engine include what is recognised as the first algorithm intended to be processed by a machine; thanks to this, she is sometimes considered the world's first computer programmer.
The computer language Ada, created on behalf of the United States Department of Defense, was named after Ada Lovelace.


"Ada Lovelace Day" is an annual event celebrated on October 16th whose goal is to "raise the profile of women in science, technology, engineering and maths". The Ada Initiative is a non-profit organization dedicated to increasing the involvement of women in the free culture and open source movements.
Wikipedia














Ada Lovelace Day: Women celebrate female scientists - BBC  

Ada Lovelace Day Celebrates Women in Science - National Geographic

Celebrate Ada Lovelace Day - CNet

Celebrating Women in Technology for Ada Lovelace Day 2012 - EFF

 

Programming Optimization and Performance

Tips, tricks, techniques for C# optimization and performance

Program optimization@wikipedia - first learn its description :)
C# Optimizations@dotnetperls
Optimization Misnomer@dotnetperls
Hidden Features of C#@stackoverflow
How to Write High-Performance C# Code
Code performance analysis in Visual Studio 2008 
Improving .NET Application Performance and Scalability@Microsoft (book, you can download) 
Writing High-Performance Managed Applications : A Primer@MSDN
Design for Performance vs. Tune for performance@MSDN
Writing Faster Managed Code: Know What Things Cost@MSDN
Performance Tips and Tricks in .NET Applications@MSDN
Best Practise for Improving .NET Application Performance and Scalability
Optimize Your Program Performance@wikihow 
http://stackoverflow.com/a/927773 - how a performance tuning can be done
http://stackoverflow.com/a/2473829
Performance@MSDN
Code optimization tutorial@CodeProject Part I - Part II - Part III 


You can use some profiling tools.

BONUS
Grace Hopper To Programmers: Mind Your NanoSeconds 

"The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." (Michael A. Jackson

Nao

Aldebaran Robotics - Nao




Official Site

StringBuilder vs String Concat

Lets look at StringBuilder class and string '+' operator performances. I wrote a small program that compares execution times and memory usage.

This program concats strings with variety sizes for 1, 10, 100, 1000, 10000, 100000, 1000000 times.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace Test
  8. {
  9.     class Program
  10.     {
  11.         static string[] data = {"a", "ab", "abc", "abcd", "abcde",
  12.                                 new string('f',10),
  13.                                 new string('g',15),
  14.                                 new string('h',20),
  15.                                 new string('i',25),
  16.                                 new string('j',30)};
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             StringTest();
  21.             //StringBuilderTest();
  22.             //StringMemoryTest();
  23.             //StringBuilderMemoryTest();
  24.  
  25.             Console.ReadLine();
  26.         }
  27.  
  28.         static void StringTest()
  29.         {
  30.             Stopwatch sw = new Stopwatch();
  31.  
  32.             string collector = string.Empty;
  33.             int count = 1;
  34.  
  35.             Console.WriteLine("Loop        Time");
  36.  
  37.             for (int i = 1; i <= 7; i++)  //1, 10, 100, 1000, 10000, 100000, 1000000
  38.             {
  39.                 sw.Start();
  40.  
  41.                 for (int j = 0; j < count; j++)
  42.                 {
  43.                     collector = collector + data[j % 10];
  44.                 }
  45.  
  46.                 sw.Stop();
  47.                 TimeSpan ts = sw.Elapsed;
  48.                 Console.WriteLine("{0,-12:d}{1:0} ms", count, ts.TotalMilliseconds);
  49.  
  50.                 count *= 10;
  51.                 collector = string.Empty;
  52.  
  53.                 sw.Reset();
  54.             }
  55.         }
  56.  
  57.         static void StringBuilderTest()
  58.         {
  59.             Stopwatch sw = new Stopwatch();
  60.  
  61.             StringBuilder collector = new StringBuilder();
  62.             int count = 1;
  63.  
  64.             Console.WriteLine("Loop        Time");
  65.  
  66.             for (int i = 1; i <= 7; i++)  //1, 10, 100, 1000, 10000, 100000, 1000000
  67.             {
  68.                 sw.Start();                
  69.  
  70.                 for (int j = 0; j < count; j++)
  71.                 {
  72.                     collector.Append(data[j % 10]);
  73.                 }
  74.  
  75.                 sw.Stop();
  76.                 TimeSpan ts = sw.Elapsed;
  77.                 Console.WriteLine("{0,-12:d}{1:0} ms", count, ts.TotalMilliseconds);
  78.  
  79.                 count *= 10;
  80.                 collector = new StringBuilder();
  81.  
  82.                 sw.Reset();
  83.             }
  84.         }
  85.  
  86.         static void StringMemoryTest()
  87.         {
  88.             string collector = string.Empty;
  89.             int count = 1;
  90.             long m = 0;
  91.  
  92.             Console.WriteLine("Loop        Memory");
  93.  
  94.             for (int i = 1; i <= 7; i++)  //1, 10, 100, 1000, 10000, 100000, 1000000
  95.             {
  96.                 for (int j = 0; j < count; j++)
  97.                 {
  98.                     collector = collector + data[j % 10];
  99.                 }
  100.  
  101.                 //m = GC.GetTotalMemory(false);
  102.                 m = Process.GetCurrentProcess().PrivateMemorySize64;
  103.                 Console.WriteLine("{0,-12:d}{1:0} kb", count, m / 1024);
  104.  
  105.                 count *= 10;
  106.                 collector = string.Empty;
  107.             }
  108.         }
  109.  
  110.         static void StringBuilderMemoryTest()
  111.         {
  112.             StringBuilder collector = new StringBuilder();
  113.             int count = 1;
  114.             long m = 0;
  115.  
  116.             Console.WriteLine("Loop        Memory");
  117.  
  118.             for (int i = 1; i <= 7; i++)  //1, 10, 100, 1000, 10000, 100000, 1000000
  119.             {
  120.                 for (int j = 0; j < count; j++)
  121.                 {
  122.                     collector.Append(data[j % 10]);
  123.                 }
  124.  
  125.                 //m = GC.GetTotalMemory(false);
  126.                 m = Process.GetCurrentProcess().PrivateMemorySize64;
  127.                 Console.WriteLine("{0,-12:d}{1:0} kb", count, m / 1024);
  128.  
  129.                 count *= 10;
  130.                 collector = new StringBuilder();
  131.             }
  132.         }
  133.     }
  134. }

Here are the results

Execution time with string.
After 10 minutes i terminated the program.

 Execution time with stringbuilder

Memory Usage - String

Memory Usage - StringBuilder


Stopwatch@MSDN

GC.GetTotalMemory@MSDN

PrivateMemorySize64@MSDN

Optimizing C# String Performance - Good article to understand string

Happy coding

3G vs 4G Speed Test

While 3G estimated max speed is 3Mbit/s, 4G can go up to 100 Mbit/s Here are some speed tests.



Get Adobe Flash player

A detailed comparision

ASUS Padfone

Phone in tablet. An awesome design from ASUS after Taichi, laptop-tablet hybrid with two screens.




Padfone - official site

Microsoft Visual Studio / Tips 1

These will make your life easier :)
 
Line Numbers
Go to Tools - Options - Text Editor. Select your language.















Immediate Window
Debug - Windows - Immediate Window
You can execute commands, define variables, classes and call your methods. You dont need to run your application. Detail Information - MSDN
















Todo List
Start your comments with "Todo:"  These comments will be showed at Task List window. The comments with start these special words in all over your solution are showed. There is also Hack and Undone markers.
To open Task List window go to View - Task List. Choose "Comments" from dropdown menu. If you choose "User Tasks", you can create tasks, assign priority and mark them as completed.
Detail information - MSDN













Refactor
Go to Refactor menu or right click and choose Refactor.
For example you can easily create properties from fields.

Custom Keyboard Shortcuts
Tools - Options  - Environment - Keyboard - Assign your favorite keys















Customize Your Toolbars and Menu Items
Tools - Customize
Here you can arrange toolbars and menu items.



















Code Snippets
You can easily insert pre-defined code blocks. You can design your own code snippets
Detail information - MSDN
Snippet Designer
Code Project - Create Your Own Snippets

Right click and choose Insert Snippet
Edit - IntelliSense  - Insert Snippet
Right shortcut of snippet and ENTER
 
















Open Complete Word Window ShortCut
ALT GR + Left Arrow or CTRL + Space

Complete Word Window Transparent

Press CTRL or ALT GR and hold for a while.
Its very annoying when you cant see the code behind it.

 













To be continued...

Happy coding

Connect MySql Database with C#

First go to MySQL site and download MySQL Connector.
After you installed it, you are ready to go. Its that simple and easy.
Here is an example

Add Mysql.Data assembly reference

  1. using System.Data;
  2. using MySql.Data.MySqlClient;
  3.  
  4. public DataTable GetDataFromMySql()
  5. {          
  6.     DataSet ds = new DataSet();
  7.     MySqlDataAdapter da;
  8.     MySqlConnection con = new MySqlConnection("Server=your_server;Database=your_db;Uid=your_username;Password=your_password");
  9.  
  10.     try
  11.     {
  12.         MySqlCommand cmd = new MySqlCommand("select * from your_table", con);
  13.         con.Open();
  14.         da = new MySqlDataAdapter(cmd);
  15.         da.Fill(ds);                
  16.     }
  17.     catch
  18.     {
  19.         throw;
  20.     }
  21.     finally
  22.     {
  23.         if (con.State == ConnectionState.Open)
  24.             con.Close();
  25.     }
  26.  
  27.     return ds.Tables[0];
  28. }


Happy Coding

C# Call Constructor From Constructor

  1. public class A
  2. {
  3.     public A() { }
  4.  
  5.     public A(int i) : this() { }
  6.  
  7.     public A(int i, string s) : this(i) { }
  8. }
  9.  
  10. public class B : A
  11. {
  12.     public B() { }
  13.  
  14.     public B(int i) : base() { }
  15.  
  16.     public B(int i, string s) : base(i, s) { }
  17. }

Class A constructors call itself constructor by using this keyword. B is derived from A. Constructors of B call base constructors using base keyword.

Happy coding

C# Password Generator

  1. public static string GeneratePassword(int minLen, int maxLen, bool isUpperCase = true, bool isLowerCase = true, bool isNumber = true, bool isSymbol = true)
  2. {
  3.     string allowedChars = string.Empty;
  4.     if (isUpperCase)
  5.         allowedChars += "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
  6.     if (isLowerCase)
  7.         allowedChars += "abcdefghiklmnopqrstuvwxyz";
  8.     if (isNumber)
  9.         allowedChars += "1234567890";
  10.     if(isSymbol)
  11.         allowedChars += "!'^#+$%&/{([)]=}*?\\_-@~,;:<>";
  12.  
  13.     Random rd = new Random();
  14.     StringBuilder sb = new StringBuilder();    
  15.            
  16.     int len = rd.Next(minLen, maxLen);
  17.     int randomIndex;
  18.     char randomChar;
  19.     for (int i = 0; i <= len; i++)
  20.     {
  21.         randomIndex = rd.Next(allowedChars.Length);
  22.         randomChar = allowedChars[randomIndex];
  23.         sb.Append(randomChar);
  24.     }
  25.  
  26.     return sb.ToString();
  27. }

Here is the result for min length = 5 and max length = 15
rsN\&,Juk
~1-?Q'~Mku
wFnElGP:L+8sA
FN#'(i
Q6oid5l
xe$iFlD
ft*kA;
)u-)Kx
o3L2fn>9
o%=2i-


Random object use seed from system clock. If you are going to create passwords consecutive in very small time interval, probably random.next will return same value. In this case define random object as a static field. Also you can pass it as parameter. Even you can seed random object by yourself but you must change seed value everytime you call. You can use a counter.

  1.     Random rd = new Random(seed);

C# GLOBALS

Global values are useful to store constant values of your program or transfer values between different scopes. They can be accessed anywhere. Unfortunately there is not any predefined struct in .NET . Globals is defined using static class.

  1. using System;
  2.  
  3. //Declare GLOBALS class in global namespace or wherever you want
  4. public static class GLOBALS
  5. {
  6.     public const int KEY = 5000;
  7.     public static string STATUS = "NULL";
  8. }
  9.  
  10. namespace Test
  11. {
  12.     class Program
  13.     {
  14.         public static void MyMethod()
  15.         {
  16.            //Do something
  17.  
  18.             GLOBALS.STATUS = "MYMETHOD";
  19.         }
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.             Console.WriteLine("KEY:{0,10:d}", GLOBALS.KEY);
  24.             Console.WriteLine("STATUS:   {0}", GLOBALS.STATUS);
  25.             Console.WriteLine();
  26.  
  27.             MyMethod();
  28.  
  29.             Console.WriteLine("KEY:{0,10:d}", GLOBALS.KEY);
  30.             Console.WriteLine("STATUS:   {0}", GLOBALS.STATUS);
  31.         }        
  32.     }
  33. }

C# Method Return Multiple Values

There are various ways return multiple values from method in .net .
First way Tuple, our new class comes with .net 4.0.

  1. using System;
  2.  
  3. namespace Test
  4. {
  5.     class Program
  6.     {
  7.         public static Tuple<int, string> ReturnTuple()
  8.         {
  9.             int i = 0;
  10.             string str = string.Empty;
  11.  
  12.             //Do something
  13.  
  14.             //There is two way to create a tuple object
  15.             return Tuple.Create(i, str);
  16.             //return new Tuple<int, string>(i, str);                
  17.         }
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             int myInt;
  22.             string myString;
  23.  
  24.             Tuple<int, string> myTuple = ReturnTuple();
  25.  
  26.             myInt = myTuple.Item1;
  27.             myString = myTuple.Item2;
  28.  
  29.             Console.WriteLine("{0:d}-{1}", myInt, myString);
  30.         }        
  31.     }
  32. }

Second way use ref, out keywords.
  1. using System;
  2.  
  3. namespace Test
  4. {
  5.     class Program
  6.     {
  7.         public static int UseOut(out string str)
  8.         {
  9.             int i = 0;
  10.             str = string.Empty;
  11.  
  12.             //Do something
  13.  
  14.             return i;        
  15.         }
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             int myInt;
  20.             string myString;
  21.  
  22.             myInt = UseOut(out myString);
  23.  
  24.             Console.WriteLine("{0:d} - {1}", myInt, myString);
  25.         }        
  26.     }
  27. }

You can create your custom classes or structs and return them. These classes will include return variables.
  1. using System;
  2.  
  3. namespace Test
  4. {
  5.     class Program
  6.     {
  7.         public static ReturnValues ReturnCustomStruct()
  8.         {
  9.             ReturnValues rv;
  10.             int i = 0;
  11.             string str = string.Empty;
  12.  
  13.             //Do something
  14.  
  15.             rv.ValueInt = i;
  16.             rv.ValueString = str;
  17.             return rv;        
  18.         }
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             int myInt;
  23.             string myString;
  24.  
  25.             ReturnValues rv = ReturnCustomStruct();
  26.  
  27.             myInt = rv.ValueInt;
  28.             myString = rv.ValueString;
  29.             Console.WriteLine("{0:d} - {1}", myInt, myString);
  30.         }        
  31.     }
  32.  
  33.     struct ReturnValues
  34.     {
  35.         public int ValueInt;
  36.         public string ValueString;
  37.     }
  38. }

4th way. Use List or Array
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Test
  5. {
  6.     class Program
  7.     {
  8.         public static List<object> ReturnList()
  9.         {
  10.             int i = 0;
  11.             string str = string.Empty;
  12.  
  13.             //Do something
  14.  
  15.             return new List<object> {i,str};        
  16.         }
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             int myInt;
  21.             string myString;
  22.  
  23.             List<object> list = ReturnList();
  24.  
  25.             myInt = Convert.ToInt32(list[0]);
  26.             myString = list[1].ToString();
  27.             Console.WriteLine("{0:d} - {1}", myInt, myString);
  28.         }        
  29.     }
  30. }

Join return variables string or serialization values with a separator.
  1. using System;
  2.  
  3. namespace Test
  4. {
  5.     class Program
  6.     {
  7.         public static string JoinReturnValues()
  8.         {
  9.             int i = 0;
  10.             string str = string.Empty;
  11.  
  12.             //Do something
  13.  
  14.             //separator |
  15.             string returnValue = string.Join("|", i, str);
  16.             return returnValue;
  17.         }
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             int myInt;
  22.             string myString;
  23.  
  24.             string s = JoinReturnValues();
  25.  
  26.             string[] sArray = s.Split(new char[] { '|' }, 2);
  27.             myInt = Convert.ToInt32(sArray[0]);
  28.             myString = sArray[1].ToString();
  29.             Console.WriteLine("{0:d} - {1}", myInt, myString);
  30.         }        
  31.     }
  32. }

If you are going to return two values you can use KeyValuePair class. There are some other ways too. We can use GLOBAL variables or XML.
m ant / Mechanical Ant