What is type reference?

Asked

Viewed 111 times

6

Was reading regarding the key word ref and I came across a warning that said not to confuse reference passage with type reference. No doubt this warning left me confused and awakened more doubts about the subject, as I could not see the explanation.

Question

So I’d like to know what type reference?

1 answer

4


Bad translation (I believe). You’re talking about types by reference. I talk about it in What’s the difference between Struct and Class?.

These are types that are naturally already objects pointed somewhere and that has implications for how it is used and how it behaves. I talked about this in Memory allocation in C# - Value types and reference types.

That resource ref is not new, existed since C# 1.0. It allowed you to pass types by reference, i.e., you created an alias for the data, so you had a variable in the method (it was always a parameter) that pointed to a data elsewhere, and C# ensures that this would always be local (no stack).

What’s new is that ref now it can be in a local variable that is not a parameter, it can be a return and it is possible to even create a type per value that is by reference. Confused?

Actually using a ref struct is creating a type by reference, there is no doubt about it. So the links speak of something slightly obsolete. The difference is that this is a type by reference allocated in the stack, before all reference types were allocated in the heap. This greatly reduces the pressure on Garbage Collector. Many types do not work with lifetime objects that require the heap, but they need to have a semantics of indirect And it was a problem that both things were tied up. This new feature allows for short, local, automatically managed lifespan and maintain indirect access to the data, as if it were a class.

It is a huge gain in efficiency in various scenarios. But I know it will be underutilized, because most programmers only care about functioning and being easy for them. I’ve had contact with experienced programmers who don’t care about these things. Believe me, some relatively new people who live here are already doing better than experienced things who think none of these things have value.

There is the engineer and the experienced curious.

I talked about it in What good is this 'in' in C#?. When you add that to the immutability gives a very interesting power.

Example:

public readonly ref struct RefRoPoint4D {
    public readonly double X;
    public readonly double Y;
    public readonly double Z;
    public readonly double W;
    public RefRoPoint4D(double x, double y, double z, double w) {
        X = x;
        Y = y;
        Z = z;
        W = w;
    }
}

I put in the Github for future reference.

Whenever you use this you will have a pointer in the variable that points to the real object. It cannot be used in anything that goes to heap who has longer life than his.

  • In the case of the structure RefRoPoint4D the purpose would be to pass it to a method without the intention of modification, certain?

  • Without changing in any situation, which is something that all struct should be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.