Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a string to reference another in C#

Tags:

string

c#

I'm coming from a C++ background. This question has been asked before, but try as I might I cannot find the answer. Let's say I have:

string[] ArrayOfReallyVeryLongStringNames = new string[500];
ArrayOfReallyVeryLongStringNames[439] = "Hello world!";

Can I create a string that references the above (neither of these will compile):

string a = ref ArrayOfReallyVeryLongStringNames[439];  // no compile
string a = &ArrayOfReallyVeryLongStringNames[439];     // no compile

I do understand that strings are immutable in C#. I also understand that you cannot get the address of a managed object.

I'd like to do this:

a = "Donkey Kong";  // Now ArrayOfReallyVeryLongStringNames[439] = "Donkey Kong";

I have read the Stack Overflow question Make a reference to another string in C# which has an excellent answer, but to a slightly different question. I do NOT want to pass this parameter to a function by reference. I know how to use the "ref" keyword for passing a parameter by reference.

If the answer is "You cannot do this in C#", is there a convenient workaround?

EDIT: Some of the answers indicate the question was unclear. Lets ask it in a different way. Say I needed to manipulate all items in the original long-named array that have prime indices. I'd like to add aliases to Array...[2], Array...[3], Array...[5], etc to a list. Then, modify the items in the list using a "for" loop (perhaps by passing the list just created to a function).

In C# the "using" keyword creates an alias to a class or namespace. It seems from the answers, that it is not possible to create an alias to a variable, however.

like image 274
AlainD Avatar asked May 20 '14 10:05

AlainD


2 Answers

You could create a wrapper that keeps a reference to the underlying array AND the index of the string:

public sealed class ArrayStringReference
{
    private readonly string[] _array;
    private readonly int      _index;

    public ArrayStringReference(string[] array, int index)
    {
        _array = array;
        _index = index;
    }

    public string Value
    {
        get
        {
            return _array[_index];
        }

        set
        {
            _array[_index] = value;
        }
    }

    public override string ToString()
    {
        return Value;
    }
}

Then this will work:

    string[] ArrayOfReallyVeryLongStringNames = new string[500];
    ArrayOfReallyVeryLongStringNames[439] = "Hello world!";

    var strRef = new ArrayStringReference(ArrayOfReallyVeryLongStringNames, 439);
    Console.WriteLine(ArrayOfReallyVeryLongStringNames[439]); // Outputs "Hello world!"
    strRef.Value = "Donkey Kong";
    Console.WriteLine(ArrayOfReallyVeryLongStringNames[439]); // Outputs "Donkey Kong"

You could make this more convenient to use by providing an implicit string operator so you don't have to use .Value to access the underlying string:

// Add this to class ArrayStringReference implementation

public static implicit operator string(ArrayStringReference strRef)
{
    return strRef.Value;
}

Then instead of having to access the underlying string like this:

strRef.Value = "Donkey Kong";
...
string someString = strRef.Value;

You can do this:

strRef.Value = "Donkey Kong";
...
string someString = strRef; // Don't need .Value

This is just syntactic sugar, but it might make it easier to start using an ArrayStringReference in existing code. (Note that you will still need to use .Value to set the underlying string.)

like image 168
Matthew Watson Avatar answered Sep 27 '22 17:09

Matthew Watson


The closest you can get is this:

unsafe
{
    string* a = &ArrayOfReallyVeryLongStringNames[439];     // no compile
}

Which gives an exception:

Cannot take the address of, get the size of, or declare a pointer to a managed type ('string')

So no, not possible...

Also read this MSDN article which explains what types can be used (blittable types).

like image 33
Patrick Hofman Avatar answered Sep 27 '22 17:09

Patrick Hofman