Hot File

Create Access Modifiers In C#

View: 152    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

9 point/1 review File has been tested

There are four types of access modifiers in C# those are: public, private, protected and internal

Introduction

There are four types of access modifiers in C# those are: public, private, protected and internal

Access modifiers gives the access control to the class members (methods and data) that where these class members should be used? Or where should not be used? Such as they could be only used inside its own class, they could be used anywhere outside the class or they could be used within its child or sub class etc.

The access modifiers increase security of the class members and prevent them to be invalid use or miss use.

If no access modifier is specified with any member (methods and fields / variables) the C# compiler marks them private by default

Public:

  • Any class member (method or instance field) which is declared with public access can be accessed anywhere in the code freely, as well as can be direct access by object reference outside the class and also the methods defined in other classes.

Private:

  • Any class member (method or instance field) which is declared with private access can be accessed within its class by its other class members (methods) only, it cannot be direct access by object reference outside the class and also the methods defined in other classes. The private member can only be accessed through its other class members indirectly. If no any access modifier to be set, C# sets private access modifiers to all class members by default.

Protected:

  • The protected access modifier is only used with inheritance feature. Any class member (method or instance field) which is declared with protected access can be accessed within its sub or child class in hierarchy only, a protected member works like a public modifier within inheritance but it is a private outside the hierarchy or inheritance.

Internal:

  • The internal access modifier is mostly used to the assembly or a deploy-able library.

Example: Use of public and private access modifiers :

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // This field is declared as public.
        public string StudentName;
 
        // this field is declared as private.
        private double AspMarks;
 
        /* This field is declared with no access modifier, 
           this is private by default. */
        double JqueryMarks;
 
        // Following void type method initializes a private field.
        public void SetAspMarks(double aspMarks)
        {
            this.AspMarks = aspMarks;
        }
 
        // Following return type method returns a private field.
        public double GetAspMarks()
        {
            return this.AspMarks;
        }
 
        // Following void type method initializes a private field.
        public void SetJqueryMarks(double jqueryMarks)
        {
            this.JqueryMarks = jqueryMarks;
        }
 
        // Following return type method returns a private field.
        public double GetJqueryMarks()
        {
            return this.JqueryMarks;
        }
 
    }
     
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            MarkSheet markSheet = new MarkSheet();
 
         /* Note the following field is initialized directly by an object reference because 
            it is declared as public in class.*/
            markSheet.StudentName = "ABC";
 
           // If you un-comment the following private fields, the program will generate a compile-time error.
           // markSheet.AspMarks = 80;
           // markSheet.JqueryMarks = 60;
 
            Console.WriteLine("Student name: {0}", markSheet.StudentName);
 
         /* Following statements set and get these private fields (AspMarks and JqueryMarks) 
            through these methods indirectly. */
 
            markSheet.SetAspMarks(80);
            Console.WriteLine("Asp.net marks: {0}", markSheet.GetAspMarks());
 
            markSheet.SetJqueryMarks(60);
            Console.WriteLine("Jquery marks: {0}", markSheet.GetJqueryMarks());
            Console.ReadKey();
        }
        /*
        The Output will be:
         
        Student Name: ABC
        Asp.net marks: 80
        Jquery marks: 60
       */
    }
}

For successful implementation of the object oriented programming the data members / instance fields should be declared as private and they should be accessed through public set and get methods.

Create Access Modifiers In C#

Create Access Modifiers In C# Posted on 07-09-2016  There are four types of access modifiers in C# those are: public, private, protected and internal 9/10 152

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 260    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 262    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 190    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 178    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 131    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 145    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd