Friday, 22 February 2013

OPERATORS IN C# .NET


                    OPERATORS IN C# .NET




The Assignment Operator

variable_name = expression;

int  i=45;






The ? Operator

C/C++ contains a very powerful and convenient operator that replaces certain
statements of the if-then-else form. The ternary operator ? takes the general form

Exp1 ? Exp2 : Exp3;

where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The ? operator works like this: Exp1 is evaluated. If it is true, Exp2 is evaluated
and becomes the value of the expression. If Exp1 is false, Exp3 is evaluated and its
value becomes the value of the expression. For example, in
x = 10;
y = x>9 ? 100 : 200;
y is assigned the value 100. If x had been less than 9, y would have received the value
200.





The if Statement

if(condition)
{
statement sequence
}
else
{
statement sequence
}



The if-else-if Ladder

A common programming construct that is based upon the nested if is the if-else-if ladder. It
looks like this:

if(condition)
{
statement;
}
else if(condition)
{
statement;
}
else if(condition)
{
statement;
}
...
else
{
statement;
}
The switch Statement

switch()
 {
case 1:
{
statement sequence;
break;
}
case 2:
{
statement sequence;
break;
}
case 3:
{
statement sequence
break;
}
.
.
.
default:
statement sequence
}


// Use goto with a switch.
using System;
class SwitchGoto {
static void Main() {
for(int i=1; i < 5; i++) {
switch(i) {
case 1:
Console.WriteLine("In case 1");
goto case 3;
case 2:
Console.WriteLine("In case 2");
goto case 1;
case 3:
Console.WriteLine("In case 3");
goto default;
default:
Console.WriteLine("In default");
break;
}
Console.WriteLine();
}


One good use for the goto is to exit from a deeply nested routine. Here is a simple
example:
// Demonstrate the goto.
using System;
class Use_goto {
static void Main() {
int i=0, j=0, k=0;
for(i=0; i < 10; i++) {
for(j=0; j < 10; j++ ) {
for(k=0; k < 10; k++) {
Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}
stop:
Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
}
}


Increment and Decrement
x = x + 1;
is the same as
x++;
x +=1;

++x; // prefix form
or as
x++; // postfix form


and
x = x - 1;
is the same as
x--;
x =-1;

can be written as
- -x; // prefix form
or as
x- -; // postfix form

x = 10;
y = ++x;
In this case, y will be set to .

x = 10;
y = x++;
then y will be set to .

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 1;
            Console.WriteLine("Series generated using y = x + ++x;");
            for (int i = 0; i < 10; i++)
            {
                int y = x + x++; // prefix ++
                Console.WriteLine(y + " ");
            }
        }

The output is shown here:
Series generated using y = x + x++;
2
4
6
8
10
12
14
16
18
20
Series generated using y = x + ++x;
3
5
7
9
11
13
15
17
19
21






















// Compute the sum and product of the numbers from 1 to 10.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int prod;
            int sum;
            int i;
            sum = 0;
            prod = 1;
            for (i = 1; i <= 10; i++)
            {
                sum = sum + i;
                prod = prod * i;
            }
            Console.WriteLine("Sum is " + sum);
            Console.WriteLine("Product is " + prod);
        }
    }
}

The output is shown here:
Sum is 55
Product is 3628800



// Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Double theta; // angle in radians
            for (theta = 0.1; theta <= 1.0; theta = theta + 0.1)
            {
                Console.WriteLine("Sine of " + theta + " is " +
                Math.Sin(theta));
                Console.WriteLine("Cosine of " + theta + " is " +
                Math.Cos(theta));
                Console.WriteLine("Tangent of " + theta + " is " +
                Math.Tan(theta));
                Console.WriteLine();

            }
        }
    }
}

Here is a portion of the program’s output:
Sine of 0.1 is 0.0998334166468282
Cosine of 0.1 is 0.995004165278026
Tangent of 0.1 is 0.100334672085451
Sine of 0.2 is 0.198669330795061
Cosine of 0.2 is 0.980066577841242
Tangent of 0.2 is 0.202710035508673
Sine of 0.3 is 0.29552020666134
Cosine of 0.3 is 0.955336489125606
Tangent of 0.3 is 0.309336249609623




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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          int i;
Console.WriteLine("Value\tSquared\tCubed");
for(i = 1; i < 10; i++)
Console.WriteLine("{0}\t{1}\t{2}", i, i*i, i*i*i);

           
        }
    }
}



Value Squared Cubed
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729


// Determine if a number is prime. If it is not, then
// display its largest factor.


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int num;
            int i;
            int factor;
            bool isprime;
            for (num = 2; num < 20; num++)
            {
                isprime = true;
                factor = 0;

                for (i = 2; i <= num / 2; i++)
                {
                    if ((num % i) == 0)
                    {
                        isprime = false;
                        factor = i;
                    }


                    if (isprime)
                        Console.WriteLine(num + " is prime.");
                    else
                        Console.WriteLine("Largest factor of " + num +
                        " is " + factor);
                }
            }
            }
        }
    }

   
   

The output from the program is shown here:
2 is prime.
3 is prime.
Largest factor of 4 is 2
5 is prime.
Largest factor of 6 is 3
7 is prime.
Largest factor of 8 is 4
Largest factor of 9 is 3
Largest factor of 10 is 5
11 is prime.
Largest factor of 12 is 6
13 is prime.
Largest factor of 14 is 7
Largest factor of 15 is 5
Largest factor of 16 is 8
17 is prime.
Largest factor of 18 is 9



// to get the reverse no.

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

namespace ConsoleApplication1
{
    class Program
     {
        static void Main(string[] args)
        {
            int num;
            int nextdigit;
            num = 123;
            Console.WriteLine("Number: " + num);
            Console.Write("Number in reverse order: ");
            do
            {
                nextdigit = num % 10;
                Console.Write(nextdigit);
                num = num / 10;
            } while (num > 0);
            Console.WriteLine();


        }
    }
}

   
   





ACCESS MODIFIERS IN C# .NET


     ACCESS MODIFIERS


Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.

In C# there are 3 different types of Access Modifiers.
Modifier
Description
public
There are no restrictions on accessing public members.
Private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected
Access is limited to within the class definition and any class that inherits from the class
INTERNAL

PROTECTEDINTERNAL





1. PUBLIC

 
The public keyword is an access modifier for types and type members. Public access is the most permissive access level.
 
There are no restrictions on accessing public members.
 
Accessibility: 
  • Can be accessed by objects of the class
  • Can be accessed by derived classes

EXAMPLE


PRIVATE

 
Private access is the least permissive access level.
 
Private members are accessible only within the body of the class.
 
Accessibility: 
  • Cannot be accessed by object
  • It Can be access with in the class only, with in it’s methods of that class
  • Cannot be accessed by derived classes

Example:



PROTECTED

 
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
 
A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
 
Accessibility: 
  • Cannot be accessed by object
  • By derived classes
EXAMPLES
CODING IN CLASS:
namespace WindowsFormsApplication2
{
    class Class1
    {
        protected int sum=234;
       
      
     }
    class class2 : Class1
    {
      
        public void sub()
        {
            int a=54;
            int b=65;
            int c=a+b;
            System.Windows.Forms.MessageBox.Show(c.ToString());
            System.Windows.Forms.MessageBox.Show(sum.ToString());          
        }
       
    }
}

CODING ON BUTTON:


        private void button1_Click(object sender, EventArgs e)
        {

            class2 ob = new class2();
            ob.sub();

        }










·      EVENTS IN WINDOW ARE PRIVATE
·      EVENTS IN WEB ARE PROTECTETED
·      IN WEB CLASS ARE PUBLIC
·      IN WINDOW CLASSES ARE INTERNAL