Problem using the ref keyword. Very newbie question!

Here is 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 get the impression that the ref keyword tells the computer to use the same variable, rather than create a new one.

Am I using it correctly? Should I use ref for both the line of the calling code and the actual implementation of the method?

+5
source share
4 answers

( UserInformation ) , , GetPersonalDetails , UserInformation.

ref UserInformation .

, , UserInformation, ref , GetPersonalDetails GetMachineDetails. , UserInformation null, ref.

Jon Skeet , .

+5

, , ref . , .

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";
    }
+4

, , .

, C. C class/struct, # . struct #, , ref , / struct .

C, :

UserInformation *CompleteInformation = new UserInformation();

GetPersonalDetails(&CompleteInformation);    

, ref, :

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

, FullInformation GetPersonalDetails, ref. ref .

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

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

# C :

UserInformation *CompleteInformation = new UserInformation();

GetPersonalDetails(CompleteInformation);    

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

..:-) , # , C. ..

+3

++, # . , , . ref ( , ). , null , . , ref , . , ++, , .

C/++, ref * ++. ref ** ++.

0
source

All Articles