Ref and Out Parameter in C#


Introduction

Hi all . before starting this article i wanted to say that this article is all about "Ref and Out" and this is one of the fundamental part of c# ,which play most important role in our day to day programming.


Get Started

Basically both the keywords used to return the value as a reference type. But both have different purpose in c# programming. 

Lets started ...........


Description

RefNeed to be Initialized before calling the function. 
OutValue is initialized in the called function .

If the above point is not understandable now then , Lets see the above point in action.



























See the above code .
For "Ref" we initialize variable first(val1) , then pass the value to the function and return back the result.
So Ref is used to serve two way communication.

For "Out" we just declare variable(val2) and return back the result.
So the Out is used for one way communication.

You can see the output below.







So we can see the expected output above.

There is a question comes to our mind that if we are not initializing the variable in case of "Ref" then what will happen. 
See the below in action.



You can see a compiler error saying that val1 is unassigned.


Conclusion

Ref and Out used to return the value . When we have a requirement to return a value from a method of type "void" then we can use these keyword.


Thanks........



Share:

Deep Understanding of Private Constructor


Introduction

Hi all . before starting this article i wanted to say that this article is deep understanding of "Private Access Modifier"  and the students Those who really wanted to dig into this topic,this article is for you.

Get Started

This article is specific to private modifier. We will discuss the advantages of using "Private Access Modifier",How to declare this  and why we need this even though there is other access modifiers are available.

Lets dig into this topic........


Is Private Keyword Possible in Class?

This is very Interesting question. Some may say Yes Some may say No. Lets see In action .

Fig 1

See the Fig 1 . I am getting compile time error when i tried to declare Private keyword. 

Now there is a question comes to our mind... How can we implement private to a class.

Lets see in action.


Fig 2
In fig-2 I defined a private constructor which makes the class private. So when i tried to create an object of testclass in Program class , then compile time error will show "Inaccessible due to protection level".

Benefits

  • Basically Private class is used in singleton design pattern.
  • When I wanted to restrict my class members to access outside the class then we should go for Private class.
  • When i wanted to restrict a class to create an object.


Share:

C# Access modifiers-Specifiers

C# Access modifiers/Specifiers for a class


Access modifier or Access specifier are the keywords of C# used to specify the accessibility to a type and it's members.

Discussion
  1. What is C# Access Modifier for a class
  2. Types of Access Modifiers
  3. Use in real time scenario
  4. Few Interview Question Associated with it
Lets discuss one by one and Understand in deep.

A.What is C# Access Modifiers  and its type?

  1. C# Modifiers is used to describe the accessibility for class and its members.
  2. This provides Security and also control the access level.

B.Types

Access Modifiers/Specifiers are of 5 types.
  1. Private
  2. Protected
  3. Internal
  4. Protected Internal
  5. Public
Private:- Private Access Specifier/Modifier used to make the class Private. But remember we can not declare class as private.
We can create a private class by using Private Constructor.

Private-class,Private-Constructor
Private Access Modifier Or Private Access Specifier

In the above Fig. we create a normal class called "Private-Class" and decorated the class with a private constructor. So the class will be considered as a private class.

Note:-

  1. The default access modifier of a constructor is Private.
  2. The default access modifier of a class variable and method are Private.

Protected:-Protected access modifier used to make the class accessible to only parent class (Inherited class).

Protected Access Modifier | Protected Access Specifier
Protected Access Modifier | Protected Access Specifier

In the above Image we can see Child Class is inheriting from the parent class . So we can access the Protected members of the parent class .

Note:-

  1. It hide class method and class variable from outside class other than parent class.
  2. We can not declare Protected to a class as an access modifier or Access Specifier. 
  3. Protected keyword can be declared to a Class variable and class member . i.e. Protected int my_int
Internal:-Internal make the class accessible to all the class which are associated with the current assembly i.e. in the same project.

Internal-Access-Modifier,Internal-Access-Specifier
Internal Access Modifier,Internal Access Specifier
In the above Image We declared 2 classes. InternalClass1 and InternalClass2. In 1st class I declared Internal to the class and class member where as in second class i did not declared any access modifier to the class.

So the InternalClass1 is accessible outside of the class where as InternalClass2 is not accessible because the access modifier of the variable and method of a class is Private,But be noted that we may or may not declare a class to Internal as this is the default access modifier.

Note :- 
1.The default access modifier of a class is Internal.
2.We may or may not declare Internal keyword before class . For example Internal Class My_class{}.

Protected Internal:- Protected Internal make the class accessible to the same assembly and the Inherited class/Parent class of other assembly.
It is the combination of both protected and Internal.

Lets take an example.

Lets create a class Library as below.

Protected-Internal-Access-Modifier | Protected-Internal-Access-Specifier

We can see we created a class  Protected_Internal_Data and Created a method called Display_Data .
The access modifier of the method is Protected Internal .

Lets access this method from another project as below.

Protected-Internal-Access-Modifier | Protected-Internal-Access-Specifier
Protected Internal Access Modifier | Protected Internal Access Specifier
We can see we can able to access the data of Display_Data() method in different assembly.
Note:- 
1.We can not declare Protected Internal to a class ,but can declare to a method and its variable.
2.Widely used when we wanted to access the inherited class from other Project or Assembly.

Public:- Public makes the class accessible to all the class which are associated with the current assembly or outside the assembly.

So this is open to all the class and all the project.

We can declare public to a Class,method and variable as an access modifiers.

Look at this with an example.

Public-Access-Modifier | Public-Access-Specifier
Public Access Modifier | Public Access Specifier

See the above Image.We declared Public to the class and class members and will try to access to another class .

See the below Image.


Public-Access-Modifier | Public-Access-Specifier
Public Access Modifier | Public Access Specifier

In the above image we can able to access the other assembly class called clsPublic.

Conclusion:-

  1. Access modifier used for restrict the accessibility.
  2. Access modifier helps to secure your class and your class members.
  3. The default access modifier of a class is Internal.
  4. The default modifier of a method, variable and constructor are Private.

Thanks.....................

Share:

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts