English 中文(简体)
不要像我这样做,我认为它应该这样做。
原标题: ref not working like I think it should

我有以下法典,在研究时,它显示,最初的我Int和我的Float在返回之前不会改变其价值。 他们的价值每当被改变在所谓的方法中时,是否应当改变,因为每次都采用这些方法?

class Tester
{
    public void Run()
    {
        int myInt = 42;
        float myFloat = 9.685f;
        Console.WriteLine("Before starting: 
 value of myInt: {0} 
 value of myFloat: {1}", myInt, myFloat);
        // pass the variables by reference
        Multiply( ref myInt, ref myFloat );
        Console.WriteLine("After finishing: 
 value of myInt: {0} 
 value of myFloat: {1}", myInt, myFloat);
     }
     private static void Multiply (ref int theInt, ref float theFloat)
     {
        theInt = theInt * 2;
        theFloat = theFloat *2;
        Divide( ref theInt, ref theFloat);
     }
     private static void Divide (ref int theInt, ref float theFloat)
     {
        theInt = theInt / 3;
        theFloat = theFloat / 3;
        Add(ref theInt, ref theFloat);
     }
     public static void Add(ref int theInt, ref float theFloat)
     {
        theInt = theInt + theInt;
        theFloat = theFloat + theFloat;
     }
     static void Main()
     {
        Tester t = new Tester();
        t.Run();
     }
}
最佳回答

EDIT: Okay,在你的评论中看到了描述......

如果您在(say)/Add上打一个断点。 那么,除非你重新评价这些变量,否则,你的观察变量就取得了一定变化,必须在正确的方面加以评估。 当点点被击中时,要看Calka Stack的观点,两度点点点击“Run”方法(在你即将到来的地方,那就意味着你重新思考),而你则看到价值更新。

问题回答

这是一种夸张的反常现象,根据Joon s和jamietre的评论,这些价值观已经消失。

您对以下术语进行了观察:myInt myFloat,而不是theInttheFloat,因此你不再看到其价值。

“The

你们可以支持遵守价值观的号召,或对你所关心的“expressions的四条进行监督。

“Select





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签