Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# reference member variable

In C#, is there a way to keep a reference as a member variable in an object (like an object pointer in C++), not just as a parameter?

EDIT: How can I make a pointer or reference to an object as a member variable?

like image 791
Andrew Avatar asked Jun 06 '11 14:06

Andrew


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

For what its worth, you could use an array of size 1 as a reference/pointer. This yields more readable code than creating a new class to wrap a single value type member.

public struct StructWithReferenceMember
{
    private int[] intStoredAsReference;

    public StructWithReferenceMember(int asValue, int asReference)
        : this()
    {
        IntStoredAsValue = asValue;
        intStoredAsReference = new int[] { asReference };
    }

    public int IntStoredAsValue { get; set; }
    public int IntStoredAsReference
    {
        get { return intStoredAsReference[0]; }
        set { intStoredAsReference[0] = value; }
    }
}

A similar trick can be used to attempt the highly discouraged practice of using mutable structs.

public class ReferenceProperty<T>
{
    private T[] typeReference;

    public ReferenceProperty(T value)
    {
        typeReference = new T[] { value };
    }

    public T PropertyAsValue
    {
        get { return typeReference[0]; }
        set { typeReference[0] = value; }
    }
    public T[] PropertyAsReference
    {
        get { return typeReference; }
    }
}

Then use array notation to "dereference" it.

public struct MutableStruct
{
    public int member;

    public MutableStruct(int value)
    {
        member = value;
    }
}
        ReferenceProperty<MutableStruct> referenceToValueType = new ReferenceProperty<MutableStruct>(new MutableStruct(3));
        Console.WriteLine("original value: " + referenceToValueType.PropertyAsValue.member.ToString());

        //referenceToValueType.PropertyAsValue.member = 4; // compiler error - cannot modify return value because it is not a variable
        MutableStruct copyOfStruct = referenceToValueType.PropertyAsReference[0]; // or referenceToValueType.PropertyAsValue
        copyOfStruct.member = 4;
        Console.WriteLine("original value after modifying copy: " + referenceToValueType.PropertyAsValue.member.ToString());

        referenceToValueType.PropertyAsReference[0].member = 5;
        Console.WriteLine("original value after modifying reference: " + referenceToValueType.PropertyAsValue.member.ToString());

original value: 3
original value after modifying copy: 3
original value after modifying reference: 5
like image 177
AdIch Avatar answered Sep 19 '22 04:09

AdIch