Sunday, April 24, 2016

Max and min Methods in c#



Max Method – Max method return larger value, max methods receives two numbers and return larger of the two arguments. The main advantage of using max method is that we can replace many if-statements with one line of code.

Example : -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Max());
            Console.ReadKey();

        }
    }
}

Output : 2
Min Methods – Min method is used to find the minimum value from the collection .The example of the min method is given below :

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

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 2, -2, -3, 0 };
            Console.WriteLine(ar.Min());
            Console.ReadKey();

        }
    }
}

Output : -3

Case In SQL

In this article I am going to explain how to use case statement in sql.

Example 1

Case <Exp>
when <Match1> then <Result1>
when <Match2> then <Result2>
else <else Result>
END

If the Exp = Match1, the output will be ‘Result1’ or else checks with the other matches and returns the corresponding Results. If it is not equal to any match the output will be 'else Result'.
 

Example

select EMPNO, Job, (Case Job
                     when 'President' then 'BIGBOSS'
                     when 'Manager' then 'BOSS'
                     when 'Analyst' then 'SCIENTIST'
                     else  'Employee'
                           End )  as Comments from Emp

OutPut



Question Write a query to generate the comments based on the salary which should be as following:
If Sal < 3000 Comment should be 'Below Target'.
If Sal = 3000 Comment should be 'On Target'.
If Sal > 3000 Comment should be 'Above Target'.

Answer

select Empno, Sal, (Case Sign (Sal-3000)
                when 1 then 'Above Target'
                when 0 then 'On Target'
                when -1 then 'Below Target'
            End )  as Comments from Emp


Example 2

Case
when <condition> then <Result>
when <condition> then <Result>
else <else Result>
End
 
select Empno, Sal (Case 
    when Sal < 3000 then 'Below Target'
                               when Sal = 3000 then 'On Target'
                               when Sal > 3000 and Sal <= 1000   then 'Above Target'
                               when Sal > 5000 then 'Very High Target'
                         End ) as Comments from Emp

 

Wednesday, April 20, 2016

Operators Precedence in C #


Category
Operator
Associativity
Postfix
()[]->.++--
Left to right
Unary
+-!~++--(type)*sizeof
Right to left
Multiplicative
*/%
Left to right
Additive
+-
Left to right
Shift
<<>> 
Left to right
Relational
<< = >> =
Left to right
Equality
==!=
Left to right
Bitwise AND
&
Left to right
Bitwise XOR
^
Left to right
Bitwise OR
|
Left to right
Logical AND
&&
Left to right
Logical OR
||
Left to right
Conditional
?:
Right to Left
Assignment
=+=-=*=/=%=>>=<<=&=^=|=
Right to left
Comma
,
Left to right

C # operators



An operator is the symbol that tells the compiler to perform mathematical or logical operations. There are different kinds of operators is given below:

Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc. Operators

Arithmetic Operators
If a=10,b=5



Operator
Description
Example
         +
Adds Tow operands
a+b=15
        -
Subtract second to first operands
a-b=5
        *
Multiply Both operands
a * b=50
        /
Divided numerator by de-numerator
a/b=2
       %
Reminder after integer division
a%b=0
      ++
Increase integer value one by one
a++=11
     --
Decrease integer value one by one
a--=9

Relational operators-
 If A=10, B=5
Operator
Description
Example
==
Checks the value of two operands is equal or not
A==B(it is not true)
!=
Checks the value of two operands is not equal
A!=B(It is true)
Checks the value of left operand is greater than value of right operand
A>B(It is true)
Checks the value of left operand is greater than value of right operand
A<B(It is not true)
>=
Checks the value of left operand is greater than or equal to the value of right operand
A>=B(It is true)
<=
Checks the value of the left operand is less than or equal to the value of right operand
A<=B(it is not true)

Logical operator-
Operator
Description
Example
&&
It is called AND operator the condition becomes true if both operand are non-zero
A&&B (false)
||
It is called OR operator the condition becomes true if any of the two operand is non-zero
A||B(true)
!
`it is called Logical NOT operator, if the condition is true then Logical NOT operator will make false
!(A&&B) true

Bitwise Operator -
Bitwise operator work on the bit, the truth table for &, |, ^ is given below:
A
B
A&B
A|B
A^B
0
0
0
0
0
0
1
0
1
1
1
1
1
1
0
1
0
0
1
1


Assignment Operator



Operator
Description
Example
=
Use to assign the values from right side operand to left side operand
C=A+B assign the value of A+B to C
+=
It adds right operand to left Operand and assign the value(result) to the left operand
A+=B is equal to A=A+B
-=
It subtract right operand from left operand and assign the result to left operand
A-=B is equal to A=A-B
*=
It multiply right operand to left operand and assign the value to left operand
A*=B is equal to A=A*B
/=
It divides right operand to left operand and assign the value to left operand
A/=B is equal to A=A/B
%=
It is used to find the modulus of two operand the assign the value to the left operand
C%=A is equal to C=C%A
<<==
Left shift AND assignment operator
A<<==2 is equal to A=A<<==2
>>==
Right shift AND assignment operator
A>>==2 is equal to A=A>>==2
&=
It is called Bitwise AND assignment operator
A&=2 is equal to A=A&2
^=
It is called bitwise exclusive OR and assignment operator
A ^=2 is equal to A=A ^2
|=
It is called bitwise inclusive OR and assignment operator
A|=2 is equal to A=A|2


Miscellaneous operator



Operator
Description
Example
Sizeof()
It return the size of a data type
Sizeof(int), return 4
typeof()
It return the type of class
Typeof(StreamReader);
&
Return the address of an variable
&a
*
Pointer to variable
*a
?:
Conditional Expression
If condition is true? Then value of X otherwise value of Y