Monday, November 23, 2015

Passing By Value Vs By Ref C#

A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable. If you want the called method to change the value of the parameter, you must pass it by reference, using the ref or out keyword. For simplicity, the following examples use ref.

Bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort


Class, delegate, dynamic, interface, object, string

C# Example
private void button1_Click(object sender, EventArgs e)
{
     int i = 100;

     PassByVal(i);   // Don't change original value
     button1.Text = i.ToString(); // OutPut is : 100

     PassByRef(ref i);   // Change original value
     button1.Text = i.ToString(); // OutPut is : 110
}

private void PassByRef(ref int i) { i += 10; }

private void PassByVal(int i) { i += 10; }

More Difference

Value Type
Reference Type
They are stored on stack
They are stored on heap
Contains actual value
Contains reference to a value
Cannot contain null values. However this can be achieved by nullable types
Can contain null values.
Value type is popped on its own from stack when they go out of scope.
Required garbage collector to free memory.
Memory is allocated at compile time
 Memory is allocated at run time

34 comments:

Haritha said...

Wonderful article, very useful and well explanation. Thanks a lot for offering this unique post with us. I really enjoyed by reading your blog post.

Hadoop Training in Chennai

janani said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
Oracle training in Chennai

Java training in Chennai | Java training in Annanagar

Java training in Chennai | Java training institute in Chennai | Java course in Chennai

Java training in Chennai | Java training institute in Chennai | Java course in Chennai

nivatha said...

Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!

Data Science course in rajaji nagar
Data Science with Python course in chenni
Data Science course in electronic city
Data Science course in USA
Data science course in pune | Data Science Training institute in Pune

Anbarasan14 said...

Thanks for sharing this information. This is really useful. Keep doing more.

Franchise Business In India
Education Franchise
Computer Education Franchise
Education Franchise India
Computer Education Franchise In India
Language Education Franchise
Spoken English Franchise In India

sathya shri said...

I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
angularjs-Training in sholinganallur

angularjs-Training in velachery

angularjs-Training in pune

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in btm

sandhiya said...

Data Science Training Institutes in Bangalore
Data Science Certification Bangalore
best analytics courses in bangalore
best data analytics courses in bangalore
big data analytics certification in bangalore

Vicky Ram said...

Nice blog, more informative keep sharing

Article submission sites
Technology

tamilsasi said...

I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium interview questions and answers

Basudev said...

Great post, thanks for the info

Telegram group links

Basudev said...
This comment has been removed by the author.
Fasts News said...

Amazing Work Thank You Very Much
See My Website

메이저토토사이트

Its VR Creation said...

Thanks for sharing the post

CyberLink PowerDirector Pro APK
U Dictionary Mod Apk Unlock [No Ads]
Nova Launcher Prime Apkt
YoWhatsapp Latest Version
ExpressVPN Mod Apk Latest

Susmita Sen said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
Download CCC Certificate

Susmita Sen said...

Really very nice blog information for this one and more technical skills are improving, I like that kind of post.
Swachhata par Nibandh

latchu kannan said...

excellent one..nice,creative..Thanks for the Giving the Good content to making valuable more. Appreciating all of your efforts to giving such an informative Blogs.

latchu kannan said...



amazing
BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

https://www.acte.in/angular-js-training-in-chennai
https://www.acte.in/angular-js-training-in-annanagar
https://www.acte.in/angular-js-training-in-omr
https://www.acte.in/angular-js-training-in-porur
https://www.acte.in/angular-js-training-in-tambaram
https://www.acte.in/angular-js-training-in-velachery


nizam said...

It is an valuable site. thanks for the info.
AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery


హాట్‌గర్ల్స్ said...


Thanks for your post! Really interesting blogs.

Digital marketing company | Digital Marketing Agency | Digital Marketing Companies in Bangalore
Digital marketing agency in hyderabad | Digital marketing companies in hyderabad

sushmi reddy said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us...
Oracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore

Jayalakshmi said...

I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
hardware and networking training in chennai

hardware and networking training in tambaram

xamarin training in chennai

xamarin training in tambaram

ios training in chennai

ios training in tambaram

iot training in chennai

iot training in tambaram

deiva said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

hema said...

thanks for posting such a wonderfull article
Web design Training in Chennai

Web design Training in Velachery

Web design Training in Tambaram

Web design Training in Porur

Web design Training in Omr

Web design Training in Annanagar

Quickbooks Expert said...

Nice & Informative Blog !
To get support for payroll update, installation, and download error, call us at QuickBooks Customer Support Number 1-(855) 550-7546 and get instant technical solutions to curb QuickBooks problems. Our team is highly skilled and offers 24-hour assistance to our customers.

QuickBooks Support Phone Number said...

Thanks for sharing this information. This is really useful. Keep doing more.


QuickBooks customer service number

Free CSEET Online Classes said...

You guys will be greatful to know that our institution is conducting online CS executive classes and a free CSEET classes only for you guys. If anyone is interested then feel free to contact us or visit our website for more details https://uniqueacademyforcommerce.com/

Elena James said...

How Do You Lose Currency Trading In IC Market

Rachel Ross said...

Hey! Lovely blog. Your blog contains all the details and information related to the topic. In case you are a QuickBooks user, here is good news for you. You may encounter any error like QuickBooks Error, visit at QuickBooks Customer Support Number for quick help.

NRakesh said...

Jharkhand Jac Board Books Online or E-books download and you can get them from JAC Official website. JAC Board e-Books You can get all topics and all training of JAC Board Board Books

HOME1 said...

hi thanku so much this information thanku so much
home1
visit here

Peter Schiff said...

Nice Topics.. Read My Review Of An Online Cfd Trading Broker, Aximtrade Reviews With A Focus On Their Platform And Customer Support Services. I Have Been Using Them For Over A Year And This Is My Honest, Unbiased Review.

Reshma said...


Awesome blog. Thanks for sharing such a worthy information....
Digital Marketing Course in Hyderabad
Digital Marketing Course in Gurgaon

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
AWS Training in Tambaram
AWS Training in Chennai

Matt Reeves said...

Great post. Thanks for sharing such a useful blog.
Artificial Intelligence Course in Velachery
Artificial Intelligence Course in Chennai

SwarnApp said...

Great post. Thanks for sharing such a useful blog.
Girvi Software
Girvi Software