0

Friends I have read few articles in Web and came to know that by default , function parameters are value type in c#. Is there any way I can validate this?

Please suggest a piece of code or any web reference.

Anil_Kumar
  • 19
  • 3
  • Neither, or rather that's not how .NET works. *Types* are reference or value types, which means they are passed by reference or value. A string is a reference type while an `Int32` is a value type. You *can* pass a value type by reference using the `ref` keyword but that isn't common – Panagiotis Kanavos Oct 18 '16 at 16:25
  • @PanagiotisKanavos Reference types [_are not_ passed by reference](http://stackoverflow.com/a/1381887/1401257)! – MAV Oct 20 '16 at 07:14
  • @MAV a reference to the data is passed, not the data itself. That's what this question is about *and* what the linked answer says. – Panagiotis Kanavos Oct 20 '16 at 09:30
  • Possible duplicate of [Reference type still needs pass by ref?](http://stackoverflow.com/questions/1381881/reference-type-still-needs-pass-by-ref) – Panagiotis Kanavos Oct 20 '16 at 09:31
  • @Anil_Kumar reference types in C# behave like references in C++. Even though a C++ reference is copied by value, the data it refers to is *not* copied. – Panagiotis Kanavos Oct 20 '16 at 09:33
  • 1
    @PanagiotisKanavos Yes. But the reference is passed by value. It is incorrect to say that reference types are passed by reference. "Pass by reference" has a specific meaning. The only reason I bring it up, is because I have seen quite a few beginners confused about the `ref` and `out` keywords, because people keep telling them that reference types are passed by reference. – MAV Oct 20 '16 at 10:00
  • @MAV when people ask whether something is passed by value or by reference, they mean whether the data is copied or not. What confuses people is actually saying that reference types are passed by value but it's really a reference, just like any other language ( you can't change the original value of a C pointer or C++ reference, you'd have to pass a pointer to the pointer). You could say that a *reference* is passed by value – Panagiotis Kanavos Oct 20 '16 at 10:07
  • @PanagiotisKanavos I do say the reference is passed by value. Anyway, let's agree to disagree. – MAV Oct 20 '16 at 10:08

1 Answers1

0

Types in c# can be value or reference types.So while passing the parameter you can pass it as say just variable(value type) or reference type using out,array,ref keywords.

//Value type
sum(a,b);
//reference type
sum(out a,out b);

In simple words reference changes are reflected throughout the scope of the variable but for value types its within function scope. To conclude,if you don't specify the kind of parameter (default) then they are value types else based on the kind say out or ref ,they are reference types.

Hameed Syed
  • 3,939
  • 2
  • 21
  • 31