Monday, March 28, 2016

factorial number

  1st method:-

int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: " + fact);

2nd method:-
class Program
        {

            static int Fact(int n)
            {
                if (n <= 1)
                    return 1;
                return n * Fact(n - 1);
            }

            static int Factorial(int n)
            {
                if (n <= 1)
                    return 1;
                int result = 1;
                for (int i = 2; i <= n; i++)
                {
                    result = result * i;
                }
                return result;
            }


            static void Main(string[] args)
            {
                Console.Write("Enter a Number to find factorial: ");
                int n = Convert.ToInt32(Console.ReadLine());
                int r = Fact(n);
                Console.WriteLine(n.ToString() + "! = " + r.ToString());

                Console.Write("Enter a Number to find factorial: ");
                n = Convert.ToInt32(Console.ReadLine());
                r = Factorial(n);
                Console.WriteLine(n.ToString() + "! = " + r.ToString());
            }
        }

3rd Method:-
  class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a number");
                int number = Convert.ToInt32(Console.ReadLine());
                long fact = GetFactorial(number);
                Console.WriteLine(fact);
                Console.ReadKey();
            }
            private static long GetFactorial(int no)
            {
                if (no == 0)
                {
                    return 1;
                }
                else
                {
                    return no * GetFactorial(no - 1);
                }
            }

        }

2nd heighest value in array

            int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
            int largest = int.MinValue;
            int second = int.MinValue;
            foreach (int i in myArray)
            {
                if (i > largest)
                {
                    second = largest;
                    largest = i;
                }
                else if (i > second)
                    second = i;
            }

            System.Console.WriteLine(second);
            int secondHighest = (from number in myArray
                                 orderby number descending
                                 select number).Distinct().Skip(1).First();

            System.Console.WriteLine(secondHighest);

max value in array

           int[] anArray = { 2, 5, 10, 9, 10, 6 };

            int? maxVal = null; //nullable so this works even if you have all super-low negatives
            int index = -1;
            for (int i = 0; i < anArray.Length; i++)
            {
                int thisNum = anArray[i];
                if (!maxVal.HasValue || thisNum > maxVal.Value)
                {
                    maxVal = thisNum;
                    index = i;
                }
            }
            Console.WriteLine(maxVal);

swap two number without temp variable

int a=10,b=30;
a=a+b;
b=a-b;
a=a-b;

Saturday, January 9, 2016

Session Management in WCF

WCF manage session by creating the instance of the service class. These created instance(s) handle the incoming service request. In WCF, session is the way of managing the services instance(s) so that server can used these instances in an optimized way. At server side InstanceContext class is used to manage service class instance.

In WCF, there are three Instance Management way as given below:
  1. Per Call

    In this way, each request is served by a new service instance. It means a new service instance is created for each request and destroyed after the request is served.This make your WCF service stateless means you can’t maintain states between WCF calls.
  2. Per Session

    In this way, each and every client requests are served by a new single service instance. It means a new service instance is created for a client all requests and destroyed after the client finished its activity.This way make your WCF service state-full means you can maintain states between WCF calls.
  3. Single

    In this way, all clients’ requests are served by a single service instance. It means a single service instance is created to handle all the clients’ requests and never destroyed after serving the request.This makes your WCF service data shared globally.

How WCF Session is different from Asp.Net Session?

WCF Session management is different from Asp.Net Session Management in the following ways:
  1. WCF Sessions are created and terminated by service client.
  2. Server use session to manage service instances, not to store some general data like Asp.Net.
  3. WCF Session does not mainly rely on Session ID like Asp.Net, a particular field with in the header or body of a group of messages can also be considered as a part of session.
  4. There may be different Session ID at client and server side unlike Asp.Net.


Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...