C# – Understanding Value vs. Reference

Hello Friends,

In today’s article we will learn what is value type and reference type in C#.

In C# the data types are classified based on the how they store the values in memory.

Let’s take the below example in C#

using System;

//Let's declare a new class called Customer
public class Customer
{
	public string Name
	{
		get;
		set;
	}
}

public class Program
{
	public static void Main()
	{
		int productCode = 104;
		Customer newCustomer = new Customer();
		newCustomer.Name = "John Doe";
		Console.WriteLine("Before->Product Id:{0}", productCode);
		//Calling the static method
		UpdateValue(productCode);
		Console.WriteLine("After->Product Id:{0}", productCode);
		Console.WriteLine("");
		Console.WriteLine("= = = = = = = = =");
		Console.WriteLine("");
		Console.WriteLine("Before->Customer Name: {0}", newCustomer.Name);
		//Calling the static method to change the customer name
		UpdateReferenceValue(newCustomer);
		Console.WriteLine("After->Customer Name: {0}", newCustomer.Name);
		Console.WriteLine("");
		Console.WriteLine("= = = = = = = = =");
	}

	//Value type example
	public static void UpdateValue(int someValue)
	{
		someValue = 204;
		Console.WriteLine("Value changed in this method:{0}", someValue);
	}

	//Reference type example
	public static void UpdateReferenceValue(Customer newCustomer)
	{
		newCustomer.Name = "Doug cooper";
		Console.WriteLine("Value changed in this method:{0}", newCustomer.Name);
	}
}

In the above code snippet we declared 2 variables an integer and a customer object types. Let’s try to understand the basics what happens in the above example.

Typically, In your laptop / desktop computer’s memory (RAM) some space is allocated for the above 2 variables, the illustration looks like below.

Value Type:

The system stores the value 104 in the location allocated for the variable productCode. When you pass a value type as a argument to method, the system creates a separate copy of that variable in that method, If we change the value inside a called function, the outer variable value does not change.

ClickHere to see full list of value types supported in C#.

Representation inside computer’s memory

Reference Type:

The system stores the value John Doe in one location in memory and points the memory location to the variable customerName. When you pass reference type as a argument to another method, the runtime framework does not create copy, instead it sends the variable address, so If we change the value of that variable in side the called method, the value also get’s updated in the calling method.

In simple terms, the reference type variable holds a pointer to another memory locations that contains the actual data.

ClickHere to see full list of reference types supported in C#.

Run the above example with the below C# editor.

Click Here to see the above Example in C# Editor

Hope this helps you and comes a quick handy tip at your work. Thank you and see you with next tip.

Please leave any feedback in the comments area. Happy Learning 🙂

Comments are not allowed for the posts older than 30 days.

Please Click Here to contact me if you have any questions.

What are 4 Pillars of OOPS?

Hello Friends,

Today, we will go through the 4 pillars of OOPS (Object Oriented Programming concepts).

These are the fundamental building blocks in software life cycle and the basic question every interviewer expects you to be aware of these concepts.

The easiest way to remember these four is to remember A PIE (SLICE) in PIZZA. The acronym “A PIE” can help you recall the four pillars of object-oriented programming.

A – ABSTRACTION : From object oriented point of view  SHOW only what is required and HIDE un-necessary information.  In simple terminology, It is hiding the un-necessary details from the user. For Example: When you switch on AC / Fan, you will get cool breeze, how the electrical energy is converted to mechanical energy to make it work is hidden from the user.

P – POLYMORPHISM : There are 2 words you need to understand here, POLY means many and MORPH means forms.  In object oriented programming world it deals with the ability of a function / method to make different forms. For Example: Visualize a person behaving differently depending on the situation, when at work and when at home. In software programming this is achieved using method overload and method override.

I – INHERITANCE : This is the easiest concept to understand, In real world we inherit some features from our parents. From programming point of view, it is the ability to base class properties and the child classes have their functionality. like the way we inherit assets and earning our own assets.

E – ENCAPSULATION : Encapsulation means data bundling allowing the data and members to be protected and exposing the public access methods using getters and setters along with the methods that operate on that data, into a single unit. It is used to hide both members and functions or methods associated with an instantiated class or object. This can be achieved using access modifiers.

To the best of my knowledge I documented here, please provide your feedback in the comments section, If you see something is not correct in this post.

4solidprinciples

Please leave any feedback in the comments area. Happy Learning 🙂

Comments are not allowed for the posts older than 30 days.

Please Click Here to contact me if you have any questions.

What are 5 SOLID Principles?

Hello Friends,

Today, we will see what are SOLID principles in Software life cycle. This is one of the must interview question for a software programmer.

In this article, we will go through over the definition for each of the principle and short description only which is needed for a quick refresh while attending the interview.

We will go through and discuss each of the principle in detail along with examples in the up-coming articles. Basically each letter denotes a specific functionality in SDLC when developing the software.

  1. S: Single Responsibility Principle (SRP) : 
      • Your function or method should not be overloaded with multiple tasks. It should be designed to handle single functionality in a given work flow.

For Example: Saving the customer information in the database, to ensure data integrity and avoid junk data, we check for duplicate customers, valid email address before saving.

            1. Check for duplicates
            2. Validate email address
            3. Calling the data access layer method to save to database 
      • SaveCustomer() – If you perform all the above 3 actions in one method, then you are violating first principle.
      • ProcessCustomer() – Design your method / function to call the below 3 private methods / functions from ProcessCustomer() to perform various tasks by different methods.
                • ValidateCustomer()
                • ValidateEmailAddress()
                • SaveCustomer()
  1. O: Open closed Principle (OSP) : 
      • A software module/class is open for extension and closed for modification. “Open for extension” means we need to design our module/class in such a way that the new functionality can be added only when new requirements are generated. “Closed for modification” means we have already developed a class and it has gone through unit testing and deployed to production as well. We should then not modify that function / method until we find bugs. As it says, a class should be open for extensions, we can use inheritance to do this.
  2. L: Liskov substitution Principle (LSP) :
      • You should be able to use any derived class instead of a parent class and have it behave in the same manner without modification
  3. I: Interface Segregation Principle (ISP) :
      • You should design your interfaces in to smaller units, clients should not be forced to implement interfaces they don’t use. Instead of one large interface group your business functionality in to smaller units, small interfaces are preferred based on groups of methods, each one serving one sub module.
  4. D: Dependency Inversion Principle (DIP) :
      • You should design your high-level modules/classes in such a way that it should not depend upon low-level modules/classes. Both should depend upon abstractions. Secondly, abstractions should not depend upon details. Details should depend upon abstractions.

All the above points are summarized in to a single picture as below. Please free to download the below image and keep in handy when you are attending interviews. 🙂 Happy Learning.

solid principles
SOLID Principles

Based on my knowledge and experience in the software field, I have written the above blog article. Click the below link for more information and details.

Internet – Truth of information and credits.

Please leave any feedback in the comments area. Happy Learning 🙂

Comments are not allowed for the posts older than 30 days.

Please Click Here to contact me if you have any questions.