Saturday, November 30, 2013

Change string type list to type integer

You can use List.ConvertAll<int>:

List<int> ints = list2.ConvertAll<int>(int.Parse);
If you don't have a list you could use a Select with int.Parse:

List<int> ints = strings.Select(s=> int.Parse(s)).ToList();

Thursday, November 28, 2013

Connection string

Below url has the connection string details of various types of database/file.

For excel :
http://www.connectionstrings.com/excel-2013/

Wednesday, November 27, 2013

Self Restarting Windows Service.


I had an situation in my Windows Service. There was a connectivity issue when dealing with the Email server. Email Server connectivity was using 3rd party .dll. and it was making productivity down. So to bring it up only solution was to restart the Service manually. To fix it automatically we need to fix in the same service writing code to restart itself. But it is not so easy. Self stop and start is not possible with out the help of another process.

Solution 1:
Creating other process telling it to stop and start the service.


                 Process proc = new Process();
                 ProcessStartInfo psi = new ProcessStartInfo();

                 psi.CreateNoWindow = true;
                 psi.FileName = "cmd.exe";
                 psi.Arguments = "/C net stop SERVERNAME && net start SERVERNAME ";
                 psi.LoadUserProfile = false;
                 psi.UseShellExecute = false;
                 psi.WindowStyle = ProcessWindowStyle.Hidden;
                 proc.StartInfo = psi;
                 proc.Start();

Solution 2:
Call, Environment.Exit with an error code greater than 0, which seems appropriate. Then on install we configure the service to restart on error.

                 Environment.Exit(1);



Friday, November 22, 2013

What does the @ character do in a path?

When you prefix a string literal with the @ symbol, you have created what is termed a verbatim string. Using verbatim strings, you disable the processing of a literal’s escape characters and print out a string as is. This can be most useful when working with strings representing directory and network paths. Therefore, rather than making use of \escape characters, you can simply write the following:

Console.WriteLine(@"C:\MyApp\bin\Debug");

Also note that verbatim strings can be used to preserve white space for strings that flow over multiple lines.

string myLongString = @"This is a very 
very 
    very 
        long string"; 
Console.WriteLine(myLongString); 

Using verbatim strings, you can also directly insert a double quote into a literal string by doubling the "token.

Console.WriteLine(@"""");

Saturday, October 19, 2013

Monday, October 7, 2013

Lazy initialization

Lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

This is typically accomplished by maintaining a flag indicating whether the process has taken place. Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot. In multithreaded code, access to the flag must be synchronized to guard against a race condition.

See lazy evaluation for a general treatment of this idea. In heavily imperative languages this pattern carries hidden dangers, as does any programming habit that relies on shared state.

 Lazy<Fruit> lazyFruit = new Lazy<Fruit>();  
 Fruit fruit = lazyFruit.Value;  

Here is a dummy example in C#.
The Fruit class itself doesn't do anything here, The class variable _typesDictionary is a Dictionary/Map used to store Fruit instances by typeName.


 using System;  
 using System.Collections;  
 using System.Collections.Generic;  
 public class Fruit  
 {  
   private string _typeName;  
   private static Dictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>();  
   private Fruit(String typeName)  
   {  
     this._typeName = typeName;  
   }  
   public static Fruit GetFruitByTypeName(string type)  
   {  
     Fruit fruit;  
     if (!_typesDictionary.TryGetValue(type, out fruit))  
     {  
       // Lazy initialization  
       fruit = new Fruit(type);  
       _typesDictionary.Add(type, fruit);  
     }  
     return fruit;  
   }  
   public static void ShowAll()  
   {  
     if (_typesDictionary.Count > 0)  
     {  
       Console.WriteLine("Number of instances made = {0}", _typesDictionary.Count);  
       foreach (KeyValuePair<string, Fruit> kvp in _typesDictionary)  
       {  
         Console.WriteLine(kvp.Key);  
       }  
       Console.WriteLine();  
     }  
   }  
   public Fruit()  
   {  
     // required so the sample compiles  
   }  
 }  
 class Program  
 {  
   static void Main(string[] args)  
   {  
     Fruit.GetFruitByTypeName("Banana");  
     Fruit.ShowAll();  
     Fruit.GetFruitByTypeName("Apple");  
     Fruit.ShowAll();  
     // returns pre-existing instance from first   
     // time Fruit with "Banana" was created  
     Fruit.GetFruitByTypeName("Banana");  
     Fruit.ShowAll();  
     Console.ReadLine();  
   }  
 }  


 

Multiton pattern

The multiton pattern is a design pattern similar to the singleton, which allows only one instance of a class to be created. The multiton pattern expands on the singleton concept to manage a map of named instances as key-value pairs.

Rather than have a single instance per application (e.g. the java.lang.Runtime object in the Java programming language) the multiton pattern instead ensures a single instance per key.

using System.Collections.Generic;  
 namespace MyApplication {  
   class FooMultiton {  
     private static readonly Dictionary<object, FooMultiton> _instances = new Dictionary<object, FooMultiton>();  
     private FooMultiton() {  
     }  
     public static FooMultiton GetInstance(object key) {  
       lock (_instances) {    
         FooMultiton instance;  
         if (!_instances.TryGetValue(key, out instance)) {  
           instance = new FooMultiton();  
           _instances.Add(key, instance);  
         }  
         return instance;  
       }  
     }  
   }  
 }  

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code