Array pointer to numbers in C#

JohnCTX 636 Reputation points
2021-01-27T19:29:59.437+00:00

This may sound strange, but can users assign an array pointer to a number; mainly an integer;

If it can be, then would users provide me some examples?

For example:

arr[0]=45;
arr[1]=18;
arr[2]=7;
arr[3]=60;
arr[4]=61;
arr[5]=10;
arr[6]=3;
arr[7]=1;

The random order would be like this:

=randombetween(arr[0]; arr[7]);

1 2 3 4 5
arr[3], arr[0], arr[2], arr[7], arr[4]

Users select 5 out of 7 array pointers:

Regards,

JohnCTX

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-01-28T06:16:57.657+00:00

    It seems that using the ref keyword can be done, creating references to references.

    But according to your description, I feel that using Dictionary<TKey,TValue> Class may be more suitable for your current situation.

                Dictionary<int, int> keyValuePairs = new Dictionary<int, int>();  
                Dictionary<int, int> dict = new Dictionary<int, int>();  
                keyValuePairs.Add(0,45);  
                keyValuePairs.Add(1,18);  
                keyValuePairs.Add(2,7);  
                keyValuePairs.Add(3,60);  
                keyValuePairs.Add(4,61);  
                keyValuePairs.Add(5,10);  
                keyValuePairs.Add(6,3);  
                keyValuePairs.Add(7,1);  
      
                Random random = new Random();  
                for (int i = 0; i < 5; i++)  
                {  
                    var keyValuePair = keyValuePairs.ElementAt(random.Next(0, keyValuePairs.Count));  
                    //To avoid selecting the same element repeatedly.  
                    keyValuePairs.Remove(keyValuePair.Key);  
                    dict.Add(keyValuePair.Key, keyValuePair.Value);  
                }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-01-27T21:05:46.017+00:00

    You can make a program do anything that is applicable to making your solution/program work.

    If you want user input to assign a number to an array based on integer elements and assigning a value using an index pointer to the array element in the array, you can do that.

    For some random order solution, you are going to have to figure that out, by using a flow chart algorithm or something before you code it. :)

    https://www.visual-paradigm.com/tutorials/flowchart-tutorial/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.