reference type parameter question

I learnt the reference type parameter passing is just a copy of the reference. If you set the passed in refernece parameter point to another object inside the called method, the orginal reference will not change.

I have a test method to test the reference type parameter passing. A refTest(SystemSwEvent systemSwEvent) method is called from that test method with a valid SystemSwEvent type object. Inside the refTest() method, the processEvScanDataAvailable(EvScanDataAvaialble systemSwEvent) method is called. Inside the processEvScanDataAvailable(EvScanDataAvaialble systemSwEvent) method, I set the passed in reference parameter to null. I expect the parameter in refTest() should not be changed. But that is not true. It will be changed to null momentarily. why?reference type parameter question

reference type parameter question

-------------Problems Reply------------

why?

I suspect this is a debugger issue, and not representative of what's actually occurring inside the CLR.

Try using different variable names for your arguments, and this behavior will go away.

The debugger recognizes the name in the current context and shows the value. It's just a coincidence that you pointed your cursor at a place that actually triggered the currently executing method (the current context).

Also note you can use the Call Stack tool to inspect the parameters of caller methods.

Object references are by default (if you don't qualify them with ref or out) passed by value, so the method is receiving a copy of the object reference - setting that copy to null doesn't change the original object reference, so what you claim to see is impossible and most likely you are just misinterpreting what you see in the debugger.

The debugger is getting confused because your parameter and variable have the same name. If you change the name of your parameter, you will notice that that debugger no longer gives information about the variable being passed into the method, but only gives information on the variable inside the method.

Simply change the name of that parameter, and you will no longer have this issue.

Category:c# Views:0 Time:2011-04-15
Tags: .net c#

Related post

  • confusion with value type and reference type in c# 2010-09-01

    I have bit confusion about parameters. When we should have to use reference parameter and when should have to use value type parameters while programming with methods/functions in c# ? --------------Solutions------------- You need to be very clear on

  • Forward Reference of Type Parameter in Java Generics 2011-03-05

    According to Java Generics FAQ http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#FAQ302 a type parameter cannot be forward-referenced in this way <A extends B, B> // error but it is ok to have <A extends List<B>

  • Does it make sense to pass a "reference type" to a method as a parameter with 'ref' key? 2011-05-11

    Possible Duplicate: C#: What is the use of “ref” for Reference-type variables? Hi, Does it make sense to pass a "reference type" to a method as a parameter with 'ref' key? Or it is just nonsense as it is already a reference type but not a value type?

  • Can a Scala type parameter reference itself when used as a parameter to a base-class? 2011-08-27

    If I have the following Scala type hierarchy: // Base traits trait TA[X <: TA[X,Y], Y <: TB[X,Y]] trait TB[X <: TA[X,Y], Y <: TB[X,Y]] trait TC[X <: TA[X,_]] // More specific traits trait TI[X <: TI[X,Y], Y <: TJ[X,Y]] extends TA

  • C# Out parameter question: How does Out handle value types? 2009-01-12

    UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref. Anything that says 'ref' I really meant by reference as in SomeMethod(Object someObject) Versus SomeMethod(out someObject) Sorry. Just don't want to change the code s

  • Passing references type variable as method parameter 2010-01-29

    After running the code below I get this output: Eve 1200 Could anyone explain me why the value of Person type variable is being changed and value of Integer type variable is not? I have already read this: www.javaworld.com/javaworld/javaqa/2000-05/03

  • Is there any way to constrain a type parameter to EITHER a reference type OR a nullable value type? 2010-07-08

    It'd be nice if I could define a method like this: public T GetItem<T>() where T : null { if (someCondition<T>()) return someCalculation<T>(); else return null; } Then I could use this on reference types (e.g., object, string) as we

  • Questions regarding object reference type casting in C#? 2010-11-02

    I have a question regarding Object type casting. Suppose we have: A a = new A(); Object o = a; As I know, what happens behind is that the compiler will copy the address of a and store in the memory area of o variable. Then,we can say that a and o are

  • Java Generics Question: type parameter E is not within its bound 2011-01-23

    i've got a question about generics. I have this method which does not compile at all. The compiler tells me: type parameter E is not within its bound. I've usually no problem in understanding compiler errors, but this one is quite tricky. Maybe my kn

  • Java reference-type in parameter 2011-03-20

    Why doesn't this change the initial object: public class Foo { Bar item = new Item(5); public changeBar(Bar test){ test = new Item(4); } public run(){ changeBar(item); } } In this case item doesn't get changed. How is this possible? I mean, you're pa

  • C# Passing reference type directly vs out parameter 2012-03-26

    I have two methods: function void A(List<int> nums) { nums.Add(10); } function void B(out List<int> nums) { nums.Add(10); } What is the difference between these two calls? List<int> numsA = new List<int>(); A(numsA); List<i

  • Why reference type is not assigned a value when I pass it as parameter? 2012-04-27

    Here, Person is a class, hence a reference type, it should be changed when I pass it as parameter in another function and assign it there. Why program.person is null here after call to ChangePerson ? namespace IndirectlyPropertySet { class Program {

  • Question about reference types 2009-02-28

    This is taken from Jon Skeet's excellent personal C# site (http://www.yoda.arachsys.com/csharp/): StringBuilder first = new StringBuilder(); StringBuilder second = first; first.Append ("hello"); first = null; Console.WriteLine (second); 1) Changing t

  • Uses for the Java Void Reference Type? 2009-03-13

    There is a Java Void -- uppercase V-- reference type. The only situation I have ever seen it used is to parameterize Callables final Callable<Void> callable = new Callable<Void>() { public Void call() { foobar(); return null; } }; Are the

  • C# supports value types and reference types, but are they all objects? 2009-07-06

    I know C# has both value and reference types, but how can you do a this: int age = 100; string blah = age.ToString(); If age is a value type, how does it have a ToString method on it? Does it get converted to an object ONLY when required internally t

  • C# string reference type? 2009-07-08

    I know that "string" in C# is a reference type. This is on MSDN. However, this code doesn't work as it should then: class Test { public static void Main() { string test = "before passing"; Console.WriteLine(test); TestI(test); Console.WriteLine(test)

  • How to use the type parameter of a generic parent inside of local classes? 2009-08-20

    Why can't I reference the type parameter of a generic parent class inside of contained local classes? public class IsGeneric<T> { public void doSomething(T arg) { class A { T x; } A foo = new A(); foo.x = arg; T bar = foo.x; // error: found jav

  • c# - ref modifier for -reference types 2009-09-21

    I am a bit confused about this piece of code. public static void Foo(A p) { p.SomeProp = "ccc"; p = null; // !!! } static void Main(string[] args) { A p = new A(); Foo(p); Console.WriteLine("SomeProp is: " + p.SomeProp); Console.ReadLine(); } The out

  • C++ reference type recommended usage 2009-09-26

    I am programming in C++ more then 5 years, and have never met any place where reference of the variable is recommended to use except as a function argument (if you don't want to copy what you pass as your function argument). So could someone point ca

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.172 (s). 11 q(s)