0% found this document useful (0 votes)
20 views27 pages

C#dotnet Unit-3 LM

This document provides an overview of classes and objects in C#, detailing their definitions, instantiation, and various types of constructors. It explains the use of the 'this' keyword and static members, as well as parameter passing techniques in C#. Example programs illustrate the concepts of defining classes, creating objects, and utilizing constructors in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views27 pages

C#dotnet Unit-3 LM

This document provides an overview of classes and objects in C#, detailing their definitions, instantiation, and various types of constructors. It explains the use of the 'this' keyword and static members, as well as parameter passing techniques in C#. Example programs illustrate the concepts of defining classes, creating objects, and utilizing constructors in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Unit – III

Classes and Objects

1. Introduction – design of classes and objects:

Class:

Formally, a class is nothing more than a user defined type (UDT) that
is composed of data (sometimes termed as attributes or instance variables)
and behaviour or functions or member functions that act on this data (often
called methods in OOP).

A Class can be said to be group of things that are considered to be at


the same conceptual level. So, as a class can be described as a set of things
that share some common features.

Defining and design of a class: Adding variables and methods

Syntax:

class <name of the class>

// methods, properties, fields, events, delegates, and nested classes.

The keyword “class” is followed by the name of the class. The opening
and closing braces contain the definition of the class. The most important
constitutes of the definition are data members and member functions. Data
members are generally implemented as variables and the member functions
as methods.

For example create a Student class with attributes name and age,
behaviour of the class contains getdata() and putdata(). Instantiate objects
to access the members of the class.
Object:

Object is an instance of a class. An object consists of instance members


whose value makes it unique in a similar set of objects.
All the objects used in C# code are of object type. An object is also known as
instance.

When an object is instantiated, it is allocated with a block of memory


and configured as per the blueprint provided by the class underlying the
object. Objects of value type are stored in stack, while those of reference
type are allocated in the heap.

For example, assume you are interested to develop a software


application for a college. At minimum, you may wish to build an entity
“Student” which is having some attributes and behaviour Figure (3.1).
Student class or entity has to maintain some attributes like name, age and
fees for each student Figure (3.2). In addition, the student class or entity
defines two methods or behaviour named getdata() and putdata(). getdata()
can be viewed as a function that takes input from the user and putdata() as
a function that displays the attributes of the class. Figure (3.3)

Instantiation:
The new operator instantiates a class by allocating memory for a new
object and returning a reference to that memory. The new operator also
invokes the class constructor.
In figure 3.4, s1 and s2 are the instances of the class Student. In the
main function creation of s1 and s2 requires the following code:

Student s1 = new Student();


Student s2 = new Student();
These statements ask the compiler to create two instances s1 and s2 of the
class Student.
Example program for defining a class and objects:
using System;
namespace StuClassobj
{
class Program
{
class Student
{
string name;
int age;
//method to set student details
public void getdata()
{
Console.WriteLine("Enter name:");
name = Console.ReadLine();
Console.WriteLine("Enter age:");
age = Int16.Parse(Console.ReadLine());
}

//method to print student details


public void putdata()
{
Console.WriteLine("Student Details: ");
Console.WriteLine("\tName:{0}", name);
Console.WriteLine("\tAge:{0}", age);
}
}
public static void Main()
{
//creating objects
Student s1 = new Student(); Instantiation of the Student class
Student s2 = new Student();
s1.getdata();
s1.putdata();
s2.getdata(); Calling functions of the Student class
s2.putdata();
}
}
}

Output:
Enter name:
ABC
Enter age:
29
Student Details:
Name:ABC
Age:29
Enter name:
XYZ
Enter age:
20
Student Details:
Name:XYZ
Age:20

Array of objects:

Array of objects contain objects as its basic units. These arrays follow
zero-based indexing, that is, the first element will have index zero. For
example, in the following declaration, the names of the various elements in
the array will be stu[0], stu[1], stu[2], stu[4] (Figure 3.5).

Syntax:

Name of the class[ ] name of the array = new Name of the class[ ];

Example:

Student[ ] stu = new Student[5];

Name of the stu is the name


[ ] indicates that it is
class of the array
an array

Stu[0] Stu[1] Stu[2] Stu[3] Stu[4]


Figure 3.5 Array of objects

Example Program for array of objects:


using System;
namespace StudentProgram
{
public class Program
{
class Student
{
string name;
int age;
public void getdata() //method to set student details
{
Console.WriteLine("Enter name:");
name = Console.ReadLine();
Console.WriteLine("Enter age:");
age = Int16.Parse(Console.ReadLine());
}
public void putdata() //method to print student details

{
Console.WriteLine("Student Details: ");
Console.WriteLine("\tName:{0}", name);
Console.WriteLine("\tAge:{0}", age);
}
}

public static void Main(string[] args)


{
Student[] s=new Student[10];
int n,i;
Console.WriteLine("Enter number of students:");
n=Int16.Parse(Console.ReadLine());
for(i=0;i<n;i++)
{
s[i] = new Student();
Console.WriteLine("Enter details");
s[i].getdata();
}
Console.WriteLine("The details of the students are as follows:");
for(i=0;i<n;i++)
{
s[i].putdata();
}
}
}
}

Output:
Enter number of students: 3
Enter details
Enter name: ABC
Enter age: 29
Enter details
Enter name: XYZ
Enter age: 20
The details of the students are as follows:
Student Details:
Name: ABC
Age: 29
Student Details:
Name: XYZ
Age: 20

Constructors and its types:

A constructor is a special method or a function of the class which gets


automatically invoked whenever an instance of the class is created. The
name of the constructor is same as that of its class of which it is a
constructor. It is used to assign initial values to the data members of the
same class. Some of the constructors supported by C# are as follows:
1. Default Constructor.
2. Parameterized Constructor.
3. Copy Constructor.
4. Private Constructor.

1. Default Constructor:

Default Constructor is a constructor that has no arguments, no return type


and has the same name as that of its class of which it is a constructor. A
default constructor has every instance of the class to be initialized to the
same values. The default constructor initializes all numeric fields to zero
and all string and object fields to null inside a class.
Example program for Default Constructor:
using System;
namespace DCon1
{
public class Program
{
public class Student
{
string name;
int age;
Student() //Default Constructor
{
name="Alice";
age=20;
}
public void putdata()
{
Console.WriteLine("Student Details: ");
Console.WriteLine("\tName:{0}", name);
Console.WriteLine("\tAge:{0}", age);
}
}
public static void Main(string[] args)
{
Student s1=new Student();
s1.putdata();
}
}
}
Output:
Student Details:
Name: Alice
Age: 20

2. Parameterized Constructor:
Parameterized Constructor is a constructor that has arguments. It has no
return type and has the same name as that of its class of which it is a
constructor. It can initialize each instance of the class to different values.
Example program for Parameterized Constructor:
using System;
namespace DCon1
{
public class Program
{
public class Student
{
string name;
int age;
Student(String s,int a) //Parameterized Constructor
{
name=s;
age=a;
}
public void putdata()
{
Console.WriteLine("Student Details: ");
Console.WriteLine("\tName:{0}", name);
Console.WriteLine("\tAge:{0}", age);
}
}
public static void Main(string[] args)
{
Student s1=new Student(“Alice”,20);
s1.putdata();
Student s2=new Student(“John”,22);
s2.putdata();
}
}
}
Output:
Student Details:
Name: Alice
Age: 20
Student Details:
Name: John
Age: 22

3. Copy Constructor:

Copy constructor is a constructor that copies the attributes of an


object to the object of which it is a constructor. It has no return type. It has
the same name as that of its class of which it is a constructor. Its main use
is to initialize a new instance to the values of an existing instance.

Note: In order to call a copy constructor we must have an object that


already has values.

The below example has a parameterized constructor, which


instantiates the first object, and a copy constructor, which copies the values
of the first object to the second object.
Example for Copy constructor:
using System;
namespace DCon1
{
public class Program
{
public class Student
{
string name;
int age;
Student(String s,int a) //Parameterized Constructor
{
name=s;
age=a;
}
public Student(Student s)
{
name=s.name;
age=s.age;
}
public void putdata()
{
Console.WriteLine("Student Details: ");
Console.WriteLine("\tName:{0}", name);
Console.WriteLine("\tAge:{0}", age);
}
}
public static void Main(string[] args)
{
Student s1=new Student(“Alice”,20);
Student s2=new Student(s1);
s2.putdata();
}
}
}

Output:

Student Details:
Name: John
Age: 22

4. Private Constructor:

If the constructor of a class is private, then it will not be possible to create


an object of that class.

Example Program for private constructor:

using System;
namespace PrivateConstructor
{
class Program
{
public class PCStudent
{
private PCStudent() //Private Constructor
{
Console.WriteLine("Private Constructor");
}
}

static void Main(string[] args)


{
PCStudent s1 = new PCStudent();
}
}
}
Output:
Error 'PrivateConstructor.Program.Student.Student()' is inaccessible due to
its protection level

“this” keyword and Static members:

“this” reference:

In c#, this keyword is used to refer the current instance of class and
by using this keyword we can pass current instance of class as a parameter
to the other methods.

In case, if class contains a parameters and variables with same name,


then this keyword is useful to distinguish between the parameters and
variables.

We can also use this keyword to declare indexers and to specify the
instance variable in the parameter list of an extension method.

In c#, we should not use this keyword to refer static field or method
and also it cannot used in static classes.

Example for “this” keyword:

1: Program using ‘this’ keyword to refer current class instance


members

using System;
namespace n1
{
class Student
{
// instance member
public string name;
public string getname()
{
return name;
}
public void setname(string name)
{
// referring to current instance
//"this.Name" refers to class member
this.name = name;
}
}
class program
{
public static void Main()
{
Student obj = new Student();
obj.setname("Welcome to New World");
Console.WriteLine(obj.getname());
}
}
}
Output:
Welcome to New World
2: Program using this() to invoke the constructor in same class:
using System;
namespace TUStudent2
{
class TUStu2
{
public TUStu2() : this("Hello")
{
Console.WriteLine("Non-Parameter Constructer Called");
}
// Declare Parameter Constructer
public TUStu2(string name)
{
Console.WriteLine("{0}",name);
}
static void Main(string[] args)
{
TUStu2 obj = new TUStu2();
}
}
}
Output:
Hello
Non-Parameter Constructer Called
3: Program using ‘this’ keyword to invoke current class method
using System;
namespace TUStu3
{
class TUStudent3
{
void display()
{
// calling fuction show()
this.show();

Console.WriteLine("Inside display function");


}
void show()
{
Console.WriteLine("Inside show function");
}
static void Main(string[] args)
{
TUStudent3 t1 = new TUStudent3();
t1.display();
}
}
}
Output:
Inside show function
Inside display function

4: Program using ‘this’ keyword as method parameter


using System;
namespace TUStu4
{
class TUStudent4
{
int a;
int b;
// Default constructor
TUStudent4()
{
a = 10;
b = 20;
}
// Method that receives 'this'
// keyword as parameter
void display(TUStudent4 obj)
{
Console.WriteLine("a = " + a + " b = " + b);
}
// Method that returns current
// class instance
void get()
{
display(this);
}
static void Main(string[] args)
{
TUStudent4 obj = new TUStudent4();
obj.get();
}
}
}
Output:
a = 10 b = 20

5: Program using this keyword to declare an indexer


using System;
namespace TUStu5
{
class TUStudent5
{
private string[] days = new string[7];
// declaring an indexer
public string this[int index]
{
get
{
return days[index];
}
set
{
days[index] = value;
}
}
static void Main(string[] args)
{
TUStudent5 g = new TUStudent5();
g[0] = "Sun";
g[1] = "Mon";
g[2] = "Tue";
g[3] = "Wed";
g[4] = "Thu";
g[5] = "Fri";
g[6] = "Sat";
for (int i = 0; i < 7; i++)
Console.Write(g[i] + " ");
}
}
}

Output:
Sun Mon Tue Wed Thu Fri Sat

Static members:
Static members are those members for which only one copy is created. If we
have a static method, then it can use only those members that are static.
Moreover, they are called by the name of the class not by the object of the
class.
Static Class
A static class is declared with the help of static keyword. A static class can
only contain static data members, static methods, and a static constructor.
It is not allowed to create objects of the static class. Static classes
are sealed, means one cannot inherit a static class from another class.
Note: Static class, methods, variables can be directly accessed with class
name without creating object to the class.
class name.variablename();
class name.methodname();
Example program for Static members:
using System;
namespace SMProgram
{
class SMProgram
{
static int i = 10;
void modify()
{
i = i + 2;
Console.WriteLine("i value inside non-static method function:{0}",i);
}
static void display()
{
Console.WriteLine("i value in static method:{0}",SMProgram.i);
}
static void Main(string[] args)
{
SMProgram s1 = new SMProgram();
s1.modify();
SMProgram.display();
}
}
}
Output:
i value inside non-static method function:12
i value in static method:12

Parameter passing techniques in C#:


In C#, arguments can be passed to parameters either by value or by
reference. Passing by reference enables function members, methods,
properties, indexers, operators, and constructors to change the value of the
parameters and have that change persist in the calling environment. To pass
a parameter by reference with the intent of changing the value, use the ref,
or out keyword. To pass by reference with the intent of avoiding copying but
not changing the value, use the in modifier.

(or)

Parameters are means of passing values to a method. There are four


different ways of passing parameters to a method in C# which are as:
1. Value
2. Ref (reference)
3. Out (reference)
4. Params (parameter arrays)

Passing parameter by value

By default, parameters are passed by value. In this method a


duplicate copy is made and sent to the called function. There are two copies
of the variables. So if you change the value in the called method it won't be
changed in the calling method.

Swapping can be performed by using either static methods or non-


static methods. If we use static method then we can call directly. It we use
non-static method we have to call that method by creating object.

Example program to perform swapping of two values using pass by


value (Using non-static method)
using System;
namespace ParPassingValue
{
class PPValue
{
void swap(int x, int y) //non-static method
{
int t;
t = x;
x = y;
y = t;
}
static void Main(string[] args)
{
PPValue p = new PPValue();
int a, b;
Console.WriteLine("Enter a value:");
a = Int16.Parse(Console.ReadLine());
Console.WriteLine("Enter b value:");
b = Int16.Parse(Console.ReadLine());
Console.WriteLine("Before swapping a:{0} b:{1}", a, b);
p.swap(a,b); //calling non-static method using object
Console.WriteLine("After swapping a:{0} b:{1}", a, b);
}
}
}
Output:
Enter a value:
54
Enter b value:
34
Before swapping a:54 b:34
After swapping a:54 b:34

Example program to perform swapping of two values using pass by


value (Using static method)
using System;
namespace ParPassingValueStatic
{
class PPSValue
{
static void swap(int x, int y) //Static method
{
int t;
t = x;
x = y;
y = t;
}
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter a value:");
a = Int16.Parse(Console.ReadLine());
Console.WriteLine("Enter b value:");
b = Int16.Parse(Console.ReadLine());
Console.WriteLine("Before swapping a:{0} b:{1}", a, b);
swap(a,b); //Calling static method directly
Console.WriteLine("After swapping a:{0} b:{1}", a, b);
}
}
}
Output:
Enter a value:
54
Enter b value:
34
Before swapping a:54 b:34
After swapping a:54 b:34

Passing parameter by reference:


Passing parameters by reference uses the address of the actual
parameters to the formal parameters. It requires “ref” keyword in front of
variables to identify in both actual and formal parameters. The process of
reference is bidirectional i.e. we have to supply value to the formal
parameters and we get back processed value.

Example program to perform swapping of two values using pass by


reference(Using static method)
using System;
namespace ParPassingSRef
{
class PPSRef
{
static void swap(ref int x,ref int y)
{
int t;
t = x;
x = y;
y = t;
}
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter a value:");
a = Int16.Parse(Console.ReadLine());
Console.WriteLine("Enter b value:");
b = Int16.Parse(Console.ReadLine());
Console.WriteLine("Before swapping a:{0} b:{1}", a, b);
swap(ref a,ref b);
Console.WriteLine("After swapping a:{0} b:{1}", a, b);
}
}
}
Output:
Enter a value:
54
Enter b value:
34
Before swapping a:54 b:34
After swapping a:34 b:54

Passing parameter by Out Parameter:


Like reference parameters, output parameters don't create a new storage
location and are passed by reference. It requires out keyword in front of
variables to identify in both actual and formal parameters.

The process of out is unidirectional i.e. we don't have to supply value to the
formal parameters but we get back processed value.

We use this process when we want some parameters to bring back some
processed values form the called method.
(or)

The out parameters are the parameters that are passed as arguments of a
method but are set by the methods themselves. They are, in fact, the
outcome of the execution of a method. They pass the result back to the
method that calls the method. The parameters are preceded by the keyword
“out”.

Example program to perform pass by out parameter (Using static


method)
using System;
namespace ParPassingSOut
{
class PPSOut
{
static void calc(int a, int b,out int x,out int y)
{
x = a * b;
y = a + b;
}
static void Main(string[] args)
{
int n1, n2,r1,r2;
Console.WriteLine("Enter a value:");
n1 = Int16.Parse(Console.ReadLine());
Console.WriteLine("Enter b value:");
n2 = Int16.Parse(Console.ReadLine());
calc(n1, n2, out r1, out r2);
Console.WriteLine("The product is:{0}",r1);
Console.WriteLine("The sum is:{0}",r2);
}
}
}
Output:
Enter a value:
23
Enter b value:
12
The product is:276
The sum is:35

Passing parameter by param (parameter arrays):

Params parameters "params" parameter is a very useful feature in C#. It is


used when we don't know the number of parameters will be passed to the
called method. Param can accept multiple values or "params" should be a
single dimensional or a jagged array.

The params keyword lets you specify a method parameter that takes an
argument where the number of arguments is variable.

No additional parameters are permitted after the params keyword in a


method declaration, and only one params keyword is permitted in a method
declaration.

Example program for pass by param (Using static method)


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

namespace ParPassingSParam
{
class PPSParam
{
class Sample
{
public void display(params int[] n)
{
foreach(int x in n)
{
Console.Write(" " + x);
}
}
}
static void Main(string[] args)
{
int[] n = {1,2,3,4,5,6};
int a = 10, b = 20, c = 30, d = 40;
Sample s1 = new Sample();
s1.display(a, b, c, d);
s1.display(n);
Console.ReadLine();
}
}
}
Output:
10 20 30 40 1 2 3 4 5 6

Passing objects to function:

An object is passed in the same way as basic data type. However, care must
be taken to ensure that the objects are initialized before passing them to a
function. The following listing passes the object of the class ABC to a
method display(). It should be noted that display() is deliberately declares as
static, so that there is just one copy of display() in the program class and the
function can be called without making an instance of POFun.

Example program to perform pass by param (Using static method)


using System;
namespace PassingObjFun
{
class ABC
{
public void xyz()
{
Console.WriteLine("Hai");
}
}
class POFun
{
static void Main(string[] args)
{
ABC a1 = new ABC();
POFun.method1(a1);
}
static public void method1(ABC A)
{
A.xyz();
}
}
}
Output:
Hai

ABC Object ABC Passed to Method1(ABC A)


class Method1(ABC A) calls XYZ
xyz() ABC a1 xyz()
Figure: Calling a Method

Basics of object oriented programming:

Object Oriented Programming (OOP) is one of the most popular


programming languages. Object oriented technology includes the following:
 Object oriented programming languages such as C# and java.
 Object oriented computer hardware.
Some of the definitions are as follows:
1. Objects:
Objects are the physical and conceptual things. They are basic run time
entities in an object oriented system. Objects are static. That is, the state of
an object will not change unless something outside the object requests the
object to change its state.
2. Classes and Meta classes:
A class is a set of all items created using a specific pattern. Therefore, the
set of all instances of that pattern is termed as a class.
Meta class is a class whose instances themselves are classes.
3. Classes and Interfaces:
A class is a set of all items created using a specific pattern. Therefore, the
set of all instances of that pattern is termed as a class.
Like a class, Interface can have methods, properties, events, and indexers
as its members. But interfaces will contain only the declaration of the
members. The implementation of interface’s members will be given by class
who implements the interface implicitly or explicitly.
4. Aggregation:
Aggregation is the process of creating a new object from two or more
objects, as it is possible for objects to be composed of other objects.
5. Specialization and Inheritance:
Specialization is the process of defining a new object based on a narrow
definition of an existing object. Specializations can be considered as
‘subclasses’ and generalizations of the specializations can be considered as a
‘superclasses’.
A class can be derived from a class is known as inheritance.
6. Polymorphism:
It is ability to take more than one form.
1. Compile time polymorphism.
2. Run time polymorphism.
7. Overloading:
Function overloading:
Having more than one function of the same name but with different number
of arguments or different types of arguments is called function overloading.
Operator overloading:
Giving a new name to an already defined operator is called operator
overloading.
8. Access levels:
Access Modifiers are keywords that define the accessibility of a member,
class or datatype in a program. These are mainly used to restrict unwanted
data manipulation by external programs or classes. There are 4 access
modifiers (public, protected, internal, private) which defines the 6
accessibility levels as follows:
 public
 protected
 internal
 protected internal
 private

9. Components of a class:
 Constructor  Properties
 Destructor  Indexers
 Constants  Operators
 Fields  Events
 Methods  Delegates
 Classes  Structures
 Interfaces
10. Properties:
Properties are the mechanisms to read and write the values of objects.
11. Indexers:
An indexer is used to access the objects with the help of the index
notation. It is written in the same way as a property. It is declared in the
following way:
public type this [int index]

Example program:
using System;
namespace TUStu5
{
class TUStudent5
{
private string[] days = new string[7];
// declaring an indexer
public string this[int index]
{
get
{
return days[index];
}
set
{
days[index] = value;
}
}
static void Main(string[] args)
{
TUStudent5 g = new TUStudent5();
g[0] = "Sun";
g[1] = "Mon";
g[2] = "Tue";
g[3] = "Wed";
g[4] = "Thu";
g[5] = "Fri";
g[6] = "Sat";
for (int i = 0; i < 7; i++)
Console.Write(g[i] + " ");
}
}
}
Visibility controls in C#:
1. Public.
2. Private.
3. Protected.
4. Internal.
5. Protected internal.
1. Public:
Public is the most common access specifier in C#. It can be access from
anywhere that means there is no restriction on accessibility. The scope of
the accessibility is inside class as well as outside. The type or member can
be accessed by any other code in the same assembly or another assembly
that references it.

Example program:

using System;
namespace ConsoleApplication1
{
class PublicAccess
{
public string msg = "This variable is public";
public void disp(string msg)
{
Console.WriteLine("This function is public : " + msg);
}
}
class Program
{
static void Main(string[] args)
{
PublicAccess pAccess = new PublicAccess();
Console.WriteLine(pAccess.msg); // Accessing public variable
pAccess.disp("Hello !!"); // Accessing public function
}
}
}
Output:
This variable is public
This function is public : Hello !!
2. Private:
The scope of the accessibility is limited only inside the classes or struct in
which they are declared. The private members cannot be accessed outside
the class and it is the least permissive access level.

Example program:

using System;
namespace ConsoleApplication1
{
class Program
{
private string msg = "This variable is private ";
private void disp(string msg)
{
Console.WriteLine("This function is private : " + msg);
}
static void Main(string[] args)
{
Program pr = new Program();
Console.WriteLine(pr.msg);// / Accessing private variable inside the
class
pr.disp("Hello !!"); // Accessing private function inside the class
}
}
}
Output:
This variable is private
This function is private : Hello !!
3. Protected:
The scope of accessibility is limited within the class or struct and the class
derived (Inherited) from this class.

Example program:

using System;
namespace ConsoleApplication1
{
class ProtectedAccess
{
protected string msg = "This variable is protected ";
protected void disp(string msg)
{
Console.WriteLine("This function is protected : " + msg);
}
}
class Program : ProtectedAccess
{
static void Main(string[] args)
{
Program pr = new Program();
Console.WriteLine(pr.msg); // Accessing protected variable
pr.disp("Hello !!"); // Accessing protected function
}
}
}
Output:
This variable is protected
This function is protected : Hello !!

4. Internal:
The internal access modifiers can access within the program that contain its
declarations and also access within the same assembly level but not from
another assembly.

Example program:

using System;
namespace ConsoleApplication1
{
class InternalAccess
{
internal string msg = "This variable is internal";
internal void disp(string msg)
{
Console.WriteLine("This function is internal : " + msg);
}
}
class Program
{
static void Main(string[] args)
{
InternalAccess iAccess = new InternalAccess();
Console.WriteLine(iAccess.msg); // Accessing internal variable
iAccess.disp("Hello !!"); // Accessing internal function
Console.ReadKey();
}
}
}
Output:
This variable is internal
This function is internal : Hello !!
5. Protected internal:
Protected internal is the same access levels of both protected and internal. It
can access anywhere in the same assembly and in the same class also the
classes inherited from the same class.

Example program:

using System;
namespace ConsoleApplication1
{
class InternalAccess
{
protected internal string msg = "This variable is protected internal";
protected internal void disp(string msg)
{
Console.WriteLine("This function is protected internal : " + msg);
}
}
class Program
{
static void Main(string[] args)
{
InternalAccess iAccess = new InternalAccess();
Console.WriteLine(iAccess.msg); // Accessing internal variable
iAccess.disp("Hello !!"); // Accessing internal function
Console.ReadKey();
}
}
}
Output:
This variable is protected internal
This function is protected internal : Hello !!

You might also like