Monday, March 31, 2014

Access Modifiers

In this article am going to discuss about access modifiers and their functionality along with, how they differ from each other and what are the basics of all these.
I’ll include all 4 access modifiers in this article.

Access Modifiers in C#

·         By default, data and methods defined in a class can only be used inside that class itself.
·         We must choose to expose them.
·         Access modifiers let you specify how programs that use your classes can access their data and methods.

 Class Access Modifiers | C#

Am going to explain this through an example-

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


namespace Hello_Word
{
    class Program

    {
       static void Main(string[] args)

                public int Value;
      // public access modifier

                private string Message;
      // private access modifier

                protected void function();
      // protected access modifier


        }
}



We can use any of the class modifiers in our class but working mechanism of all these modifiers are different.


Modifiers | Types

Public

PUBLIC modifier is used at that place in the class of which values, attributes can be easily accessed by the any other object.

Private

PRIVATE access modifier is used where we want the functionality of attribute of that class can be only used within that class only.

Protected

-          It is used where the class member can only be accessed within the same class.
-          Inherited class of that class(child/subclass)

 Internal

-         -    It is the advanced functionality of C# 5.0 or above.
-         -    In this modifier class member can be accessed by any class in the same assembly.
-         -    Package of code like a library or other program.

Example

There are 2 parts in this code; first part is representing construction building and access modifiers definitions. In the second code there is a calling of the access modifiers functionalities.

Part: 1 | Code

// Book.cs file

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

namespace Hello_Word
{
    class Book

    {
        // access modifier- PUBLIC
        public string Name;
        public decimal Price;
        public string Description;

        // access modifier- PRIVATE
        private decimal discount;

        // public constructer
        public Book(string bookName, decimal price)      

        {
            Name = bookName;
            Price = price;
            discount = 0.0m;
        }
    }
}

Part: 2 | Code

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

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

          }
     }


When you do try to define b1.discount there will be no option in the list as




But if you do try to declare, it will show an error like this


// Program.cs file

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


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

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

            decimal dsc = b2.discount;
        }
     }
}


// We can not directly use discount in our next file, because it is defined under a private class, we didn’t get the options from the list but if we do try to do so, it will surely show an error-
“Inaccessible due to private modifier”



Same in the case of protected modifier, if you try to access its properties it will again show the same message-



___________________________________________________________

For more technical blogging stuffs please visit,
http://www.c-sharpcorner.com/authors/2072a9/abhishek-jaiswal.aspx

___________________________________________________________

Sunday, March 30, 2014

Break and Continue Statements in CSharp

In this article am going to explain to of the most useful concepts of C#, these are also called loops. I’ll explain and differentiate between them through an example.


Break Statement

Break statement breaks out of the loop at current point or we can say that it terminates the loop condition.
It is represented by
break;

Continue Statement

Continue statement jumps out of the current loop condition and jumps back to the starting of the loop code.
It is represented by
continue;

Example | Break vs Continue

In this example am showing functioning of both the statements together, afterwards I’ll explain them one by one with their functionality.

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


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

            int i;        

          for (i = 0; i <= 10; i++)
          {

              if (i == 5)                             
                  continue;
             
              if (i == 8)              
                  break;

              Console.WriteLine("value is"  +i);            
          }

          Console.ReadLine();
              
            }    
        }
    }


Output Window



Explanation

In this example the out is from 0 to 7 excepting 5, 8, 9 ?

CASE: 1 It is because in the first case-

              if (i == 5)                             
                  continue;

Means it will skip the current loop and jumps to the top of the loop, so there is no 5 in the output window.

CASE: 2 In second cases-

              if (i == 8)              
                  break;

It will terminate the loop or get out of it when value becomes 8, so there is no 8, 9 in the output window.
Now let’s do some changes in the code and see what happens, here we go-

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


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

            int i;        

          for (i = 0; i <= 10; i++)
          {

             if (i == 5)                             
                  continue;
             
          //   if (i == 8)              
          //        break;

             Console.WriteLine("value is: "  +i);            
          }

          Console.ReadLine();
              
            }    
        }
    }




Now you can see the output.  Condition continues to flow according to the continue statement functioning.
Now am making some other modifications, in the code snippet, as-

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


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

            int i;        

          for (i = 0; i <= 10; i++)
          {

          //   if (i == 5)                             
          //        continue;
             
             if (i == 8)              
                  break;

             Console.WriteLine("value is: "  +i);            
          }

          Console.ReadLine();
              
            }    
        }
    }




Now you can see, it again showing from 0 to 7 and because of the break statement condition, it’s again out of the loop so there is no 8, 9, 10