Saturday 27 April 2013

C# Passing multiple parameter to a method with PARAMS

With c# param you can passing comma-separated list of arguments to your "Method".
Here i show an example with SUM method,Sum method accept a comma-separated list of arguments.

Example
    class multiple_ParameterWithParam
    {
         
        public static int SUM(params int[] val) 
        {
            int totalSum = 0;
            for (int i = 0; i < val.Length; i++) 
            {
                totalSum += val[i]; 
            }
            return totalSum;
        }

        static void Main()
        {
            Console.WriteLine("Total Sum (1-3) = {0}", SUM(1, 2, 3));
            Console.WriteLine("Total Sum (1-6) = {0}", SUM(1,2,3,4,5,6));
            Console.ReadLine();
        }
    }


Monday 22 April 2013

Introduce C# Collections

Collection that provides by .Net Framework have stacks, queues, lists, and hash tables. Collection classes are defined under System.Collections or System.Collections.Generic namespace.
At more complex, an object holds references to many other objects. The collections we often using is List and Dictionary.
Different between stacks, queues, lists, and hash tables

Declare a Dictionary
When you Create a Dictionary instance, you needed to pass in <TYPE,TYPE>,to tell that which type of Dictionary you want to Create.See Also GENERICS
Eg
 Dictionary<TYPE, TYPE> objectInstance = new Dictionary<TYPE, TYPE>();


Example(Here i'm using Dictionary)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace IntroduceCollection
{
    class IntroduceCollection
    {
        static void Main()
        {
            Dictionary<string, string> carPlateNum = new Dictionary<string, string>()
            {
                {"john", "JKE 1333"},
                {"jess", "QQ 1314"},
                {"apple", "JK 8888"},
                {"jack", "USS 1555"},
                {"alice","AC 6666"}
            };
            // Using LIST to store car OwnerName
            List<string> ownerName = new List<string>(carPlateNum.Keys);

            /* Output all the CarPlate Data *
             *                              */
            Console.WriteLine("Owner\tPlate Number");
            foreach (string name in ownerName)
            {
                Console.WriteLine("{0}\t{1}", name, carPlateNum[name]);
            }
            //end Output with using LIST

            //Direct output
            Console.WriteLine("Foreach looping Output content of Dictionary");
            foreach (KeyValuePair<string, string> carInfo in carPlateNum)
            {
                Console.WriteLine("{0}\t{1}", carInfo.Key, carInfo.Value);
            }

            Console.ReadLine();
        }
    }
} 

Example above i'm create a Dictionary to store Owner and Car Plate Number .Then output the information.

Sunday 21 April 2013

Introduce C# Generics

 Generics is a powerful feature of C#. Generics allow you to define type-safe classes without compromising type safety, performance, or productivity.

Lets think about example
Example
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace GenericIntro
{
    class GenericIntro
    {
        private class ExampleClass { }
        static void Main()
        {
            System.Collections.ArrayList list = new System.Collections.ArrayList();
            // Add an integer to the list.
            list.Add(3);
            list.Add(2);
            list.Add(6);
            // Add a string to the list. This will compile, but may cause an error later.
            list.Add("It is raining in Redmond."); //illegal

            int t = 0;
            // This causes an InvalidCastException to be returned.
            foreach (int x in list)
            {
                t += x;
            }
        }
    }
}

With this example ,we can easily add to the list, but this convenience comes at a cost.
 Code above will pass compile,but will failed at run-time.

Generics Example


  // The .NET Framework 2.0 way to create a list
List list1 = new List();

// No boxing, no casting:
list1.Add(3);

// Compile-time error:
// list1.Add("It is raining in Redmond.");

Conclusion: With Generics you can produce type safety code + reusable.

Remind: This feature is not often useful when you'r coding.You rarely need to create your generic types because .net Framework contains many useful classes already.

Reference from MSDN.
Example code from MSDN.

Objects is like arrays by using "Indexers"

I am here give some introduce about C# object indexers like array.

Define Object indexer
"this" keyword + [ ] is used for defining a object indexer.Take this as a example,you'll get a more complete example shortly.

class yourCls{
   public object this[String passIn] // Declare Indexer.
   {
     get{}
     set{}       
   }
}
 
yourCls indexerCls = new indexerCls();
indexerCls["ObjectIndexer"] = "Sample"; //you can using indexer like this.
 
Example with Collection
This example is using Collection in .Net Framework to implement Get or Set Properties dynamically.


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ObjectindexerWithCollection
{
    class DynamicProperties
    {
        public Dictionary<string, string> hData 
            = new Dictionary<string, string>(); //Declare dictionary to handler properties.

        public string this[string parameterName]
        {
            get
            {
                return this.hData[parameterName];
            }
            set
            {
                this.hData[parameterName] = value;
            }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            DynamicProperties lbx = new DynamicProperties();
            lbx["test"] = "Object ";  //set Indexer
            lbx["test1"] = "Indexers";//set Indexer

            Console.WriteLine("Here is the sample of {0}{1}", 
                lbx["test"], lbx["test1"]); //get Indexer
           
            Console.ReadLine();
        }

    }
}


Example Output
This C# feature is useful at advanced C# software development. Just take as a note.Its can be using to design internal the "software pattern", you also can Apply in your software depend on your creative thinking.

Saturday 20 April 2013

Interview Failed At Avanade

Last two week,i visit avanade for an interview.I failed because my poor english.Interviewer sayed ,your technical skill not bad,but you will got problem for english presentation.I failed because my english.Interviewer is a nice guy.He introduce me to another ms company and telling me take some english course to improve my english.After i improve my english,i will attend for interview again,hope i will pass the interview soon,good luck!

Monday 15 April 2013

MsCrm 2011 :Error Message - Assembly must be registered in isolation when register Plugin






If you found this error was being thrown because user was not a deployment administrator(maybe System Administrator).For security purpose,Mscrm will forced register the plugins under sandbox isolation mode.

You can know more at : http://msdn.microsoft.com/en-us/library/gg309620.aspx

Security Restrictions Part.