Friday, July 19, 2013

Usage of Enum in C#


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

enum days
{
    monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

namespace demo_enum
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(days.Friday);
            Console.WriteLine((int)days.Sunday);
            Console.WriteLine((int)days.Tuesday);
        }
    }
}


Output produces likes....

Friday
6
1

C# Code to generate table froms 01 - 10

C# Code to generate table froms 01 - 10 using Microsoft Visual Studio
------------------------------------------------------------------------------------------------


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


namespace Table4{

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

int prod = 0;
for (int row = 1; row <= 10; row++){

for (int col = 1; col <= 10; col++){
prod = row * col;

Console.Write(prod+"\t");}

Console.WriteLine();}

Console.WriteLine();}
}
}

.... You will get results like


Access modifiers in C#

Dot.Net Tutorials

aAccess modifiers
  • ·          Public                          Access not limited
  • ·         Private:                        Access limited to the containing of the class or a type
  • ·         Internal:                       Access limited to the containing of the program only
  • ·         Protected:                    Access limited to the containing class or types derived from the  
  •                                                                                                                    contained class
  • ·         Protected Internal:         Access limited to the program or types derived from the class


----------------------------------------------------------------------------------------------------
Example for public

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

namespace demo_18
{
   public  class test
    {
        public int x;
        public int y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            test test1 = new test();
            test1.x = 5;
            test1.y = 10;
            result = test1.x + test1.y;
            Console.WriteLine(result);

        }
    }
}

-- In the above program, class 'test' is initialized with public access modifier, hence that class is accessed by another class i.e., program

So we would get the output as

Output:

15
... press any key to continue



 Private :

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

namespace demo_18
{
   private  class test
    {
        public int x;
        public int y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            test test1 = new test();
            test1.x = 5;
            test1.y = 10;
            result = test1.x + test1.y;
            Console.WriteLine(result);

        }
    }
}

In  above namespace, demo_18 class test is intizlised with private access modifier, where the accessibility of the test class is limited to the class test only... So above program with produce error..

" Error 1 Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal D:\Suresh Babu\demo_18\demo_18\Program.cs 8 24 demo_18"