简体   繁体   English

无法使用ref关键字。 非常新手的问题!

[英]Trouble using the ref keyword. Very newbie question!

Here's my class: 这是我的班级:

public class UserInformation
    {
        public string Username { get; set; }
        public string ComputerName { get; set; }
        public string Workgroup { get; set; }

        public string OperatingSystem { get; set; }
        public string Processor { get; set; }
        public string RAM { get; set; }
        public string IPAddress { get; set; }

        public UserInformation GetUserInformation()
        {
            var CompleteInformation = new UserInformation();

            GetPersonalDetails(ref CompleteInformation);
            GetMachineDetails(ref CompleteInformation);

            return CompleteInformation;
        }

        private void GetPersonalDetails(ref UserInformation CompleteInformation)
        {

        }

        private void GetMachineDetails(ref UserInformation CompleteInformation)
        {

        }
    }

I'm under the impression that the ref keyword tells the computer to use the same variable and not create a new one. 我的印象是ref关键字告诉计算机使用相同的变量而不是创建一个新变量。

Am I using it correctly? 我正确使用它吗? Do I have to use ref on both the calling code line and the actual method implementation? 我是否必须在调用代码行和实际方法实现上使用ref?

When you pass a reference type (in your case, your UserInformation class is a reference type) as a parameter, by default you are passing the pointer to the class by value , meaning the GetPersonalDetails would get its own copy of a pointer which points to the same instance of the UserInformation class. 当您将引用类型(在您的情况下,您的UserInformation类是引用类型)作为参数传递时,默认情况下您将指针传递给类的值 ,这意味着GetPersonalDetails将获得自己的指针副本,该指针指向UserInformation类的相同实例。

Using the ref keyword in your example will pass the same pointer to the UserInformation instance to the method. 在示例中使用ref关键字将向UserInformation实例传递指向该方法的相同指针。

So in your case, if your intent is to simple modify the properties of UserInformation , the ref keyword in not needed, since you will be modifying the actual instance from GetPersonalDetails and GetMachineDetails . 因此,在您的情况下,如果您的目的是简单地修改UserInformation的属性,则不需要ref关键字,因为您将从GetPersonalDetailsGetMachineDetails修改实际实例。 However, if your intent is to instantiate a new UserInformation object from inside those methods or set the instance to null, you would need to use the ref keyword. 但是,如果您的目的是从这些方法中实例化新的UserInformation对象或将实例设置为null,则需要使用ref关键字。

Jon Skeet does a great job of explaining parameter passing in this article . Jon Skeet在解释本文中的参数传递方面做得很好。

You probably don't need to use ref here. 你可能不需要在这里使用ref Classes are reference types, so you are already passing a reference. 类是引用类型,因此您已经传递了引用。

Here's your code without ref and a number of other style issues fixed: 这是没有ref的代码,还修复了许多其他样式问题:

    public UserInformation GetUserInformation()
    {
        UserInformation userInformation = new UserInformation();

        updatePersonalDetails(userInformation);
        updateMachineDetails(userInformation);

        return userInformation;
    }

    private void updatePersonalDetails(UserInformation userInformation)
    {
        userInformation.FirstName = "Sergio";
        userInformation.LastName = "Tapia";
     }

    private void updateMachineDetails(UserInformation userInformation)
    {
        userInformation.MachineName = "Foobar";
    }

I'm under the impression that the ref keyword tells the computer to use the same variable and not create a new one. 我的印象是ref关键字告诉计算机使用相同的变量而不是创建一个新变量。

Maybe you have C background. 也许你有C背景。 Unlike C class/struct, C# class doesn't make a copy of the whole class to the called function. 与C类/结构不同,C#类不会将整个类的副本复制到被调用的函数。 If you will use struct in C#, it will make an independent copy of whole content of struct to the called function, so ref is warranted when you want to minimize the memory/runtime overhead when passing your struct data structure to your function. 如果你将在C#中使用struct,它会将struct的整个内容的独立副本复制到被调用函数,因此当你想在将struct数据结构传递给函数时最小化内存/运行时开销时,保证ref。

If you have knowledge in C, your code above translates to: 如果您对C有所了解,那么您的上述代码将转换为:

UserInformation *CompleteInformation = new UserInformation();

GetPersonalDetails(&CompleteInformation);    

And your function that has ref translates to this: 你的函数有ref转换为:

void GetPersonalDetails(UserInformation **CompleteInformation)
{
    // you can make the CompleteInformation outside to point to new instance
}

So if you will not point CompleteInformation to new instance inside of GetPersonalDetails, no need to use ref keyword. 因此,如果您不将CompleteInformation指向GetPersonalDetails中的新实例,则无需使用ref关键字。 Remove the ref keyword on calling statement and called function. 删除调用语句和被调用函数的ref关键字。

var CompleteInformation = new UserInformation();
GetMachineDetails(CompleteInformation);

private void GetPersonalDetails(UserInformation CompleteInformation)
{
    // you cannot make the CompleteInformation outside to point to new instance
}

That C# code gets translated to C as: C#代码被转换为C:

UserInformation *CompleteInformation = new UserInformation();

GetPersonalDetails(CompleteInformation);    

void GetPersonalDetails(UserInformation *CompleteInformation)
{
    // you cannot make the CompleteInformation outside to point to new instance
}

Hmm.. :-) still thinking how to explain my answer in whole C# terms, not making an analogy to C. Thinking.. 嗯... :-)仍然在思考如何用C#术语来解释我的答案,而不是对C语言进行类比。

Unlike C++, in C# passing referenced variable to a function means using the same variable. 与C ++不同,在C#中将引用变量传递给函数意味着使用相同的变量。 This means, you can change the variable and this change is visible to caller. 这意味着,您可以更改变量,并且此更改对调用者可见。 ref keyword allows to change reference itself (this means, reference is passed by reference). ref关键字允许更改引用本身(这意味着引用通过引用传递)。 This means, function may set reference to null or assign it to another object, and this change is visible to caller. 这意味着,函数可以将引用设置为null或将其分配给另一个对象,并且此更改对调用者可见。 Yes, ref should be used both by function and caller. 是的,ref应该由函数和调用者使用。 This is better than in C++, you can see in a caller code, that parameter is passed by reference. 这比在C ++中更好,你可以在调用者代码中看到,该参数是通过引用传递的。

If you know C/C++, passing reference type to function without ref keyword is the same as * in C++. 如果您了解C / C ++,则将引用类型传递给不带ref关键字的函数与C ++中的*相同。 Passing reference type to function with ref is the same as ** in C++. 使用ref将引用类型传递给函数与C ++中的**相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM