0

Before making up any question,I read one post which was about defining global array, and they suggest several solution(using list instead of array or defining static class include an array). My question is: I don't want to use list, If I wanna to define a global array which the content of this array change from one function to another function, what should i do? example, if 1 is an array,

public float[] random()
        {
          .....             
            return 1;
         }

public ......(.....)

array 1;
array 1 change into array 2;
return array 2;

public ......(.....)

array 1;
array 2;
array 3 = array 1+array 2;
return array 3;

public ......(.....)
......
user3371238
  • 67
  • 2
  • 3
  • 9
  • Why do you need it "global"? (there are no global things in C#... only static members of classes). Why not pass array/list/sequence to functions, which will return the resulted array/list/sequence? (BTW: LINQ does exactly that). – Shlomi Borovitz Mar 05 '14 at 12:39
  • 2
    If you want to do that you should rethink you design, this is not a good thing to do. Besides C# has no global variables at all, you have to pack it in a class. – Lev Mar 05 '14 at 12:39
  • 1
    Also, what advantage offer arrays over List or other Collections? – dureuill Mar 05 '14 at 12:43
  • May you clear your suggestion on my example(how can i do that by example)? @ShlomiBorovitz – user3371238 Mar 05 '14 at 12:53
  • @user3371238 `public IEnumerable DoSomething(IEnumerable sequence) { /* ... manipulating the sequence */ return result; }` `public IEnumerable DoSomethingElse(IEnumerable sequence) { /* ... manipulating the sequence in another way */ return result; }`... etc etc (sorry for the 1-line code) – Shlomi Borovitz Mar 05 '14 at 12:58
  • I have no idea what you're trying to convey with that pseudo code example. It leaves me more confused, rather than less. – Quick Joe Smith Mar 05 '14 at 13:06

2 Answers2

0

C# does not have global variables.

The closest thing to a global you could do is create a singleton class to hold the array.

Jon Skeet has a very informative article on how to implement singletons in C#.

The "preferred" method to create one in .NET 4+ is as such:

public class Foo
{
    private static readonly Lazy<Foo> Instance = new Lazy<Foo>(() => new Foo());

    private Foo()
    {
    }

    public static Foo Default
    {
        get
        {
            return Instance.Value;
        }
    }

    public static int[] Bar { get; set; }
}

As Shlomi Borovitz noted, you can use a static class too:

public static class Foo
{
    public static int[] Bar { get; set; }
}

You can find differences between singleton and static classes in the "Difference between static class and singleton pattern?" question. Mainly you'll lose the ability to lock the class (for concurrency) and injection/testability.

However, as Lev said, if you need to use globals in C# you're doing something really wrong in your architecture.

Community
  • 1
  • 1
Albireo
  • 10,977
  • 13
  • 62
  • 96
0

if you really want a global variable, you can allocate some memory, access it in an unsafe context, and pass the pointer to the memory location around. Usually you do this for performance reasons, it's not a OO best practice :)

http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx

FrankyHollywood
  • 1,497
  • 19
  • 18