0

I am new to coding and still at college at the moment. Sorry if this question has been asked before but I could not see it any where.

I was wonder if it is possible to create a constructor for the array Class, so each time I create a new array I can execute code such as count the amount of arrays I have in my application for each instance I make? I understand the Array class is abstract so you cannot make an instance of it.

Are int[] arrays just methods within the abstract Array class?

Any insight as to why it is or isn't possible would be greatly appreciated!

Thanks

Sifer
  • 57
  • 1
  • 6
  • 1
    Your question is a little too vague. "so each time I create a new array I can execute code automatically?" <-- how do you want to populate the array? In a loop? From another collection that you already have? – rory.ap Dec 02 '15 at 15:14
  • Please don't use irrelevant tags. This is a c# question - it should not have a `java` tag. – J... Dec 02 '15 at 15:16
  • 4
    Can you give an example of what you are trying to do, no matter how wrong it is? Are you wanting to work with arrays like `int[]` and `float[]`, or new objects you define like `MyObject[]`? – Ron Beyer Dec 02 '15 at 15:16
  • Maybe [this topic](http://stackoverflow.com/questions/5678216/all-possible-c-sharp-array-initialization-syntaxes) answers your question ... – frank Dec 02 '15 at 15:17
  • You could create a method that is returning an array of whatever you want. – AntiHeadshot Dec 02 '15 at 15:21
  • 1
    Well, sure, std::array has a constructor. Nobody can fix C arrays anymore. – Hans Passant Dec 02 '15 at 15:27
  • None of the given answers do what he wants; however the answer to the asked question is NO! therefore the answers as given are the best he's going to get. Voting to close. – Joshua Dec 02 '15 at 17:21
  • Thanks guys like I said I was unsure if it was possible as i'm a complete newbie at programming, I tagged Java since I thought the same process would be possible in Java since you can create arrays and constructors so its not definitive to C#. I have edited the question but vote to close as Joshua said it is not possible. Thanks anyways – Sifer Dec 02 '15 at 18:53

2 Answers2

0

If your custom code is just to populate the array, C# provides several built in ways to do this (see All possible C# array initialization syntaxes).

If you want to have code execute every time you create an instance of a particular kind of object, you should make a class that represents that object (you might be able to do an extension method as well, but that would likely get confusing over time). This class might be nothing more than a wrapper around an array:

public class MyArray<T>
{
    private T[] _array;

    public MyArray()
    {
        // execute your always must run code here!
    }

    public ArrVal
    {
       get { return _array; }
       set { _array = value; }
    }
}

...

MyArray<int> myArray = new MyArray<int>(); // your custom code gets executed when you new up the object here

However, per best practices, you should avoid having code in a constructor that does too much work (and in my experience, having a constructor that throws exceptions can cause some problems that are hard to debug, although MSDN says it's better to throw the exception than to cover it up). If this code is going to be doing intensive work, it may be better to create a separate method (maybe something called public void Initialize()) so that callers can new up the object more lazily.

You should also avoid trying to have this done for all arrays, because I can guarantee it'll cause problems for you or someone else down the road when they can't figure out why int[] arr = new int[3] is doing extra stuff. You should look to properly use encapsulation here instead (i.e. creating a wrapper/extension/decorator class).

Also, it's entirely possible that one of the existing .NET classes for Collections fulfills your needs... Look into those.

Community
  • 1
  • 1
Dan Field
  • 20,885
  • 5
  • 55
  • 71
0

I'm not sure if I understand your question correctly, but if you want to be able to execute code when you create an array you can use LINQ:

int[] myArray = new int[10]
    .Select((x, idx) => /*execute your code here*/).ToArray();

So you can create an array of length 10, then you can execute code for each element to determine what you want to populate it.

For example you could fill the array with random numbers using:

Random random = new Random();
int[] myRandomArray = new int[10]
    .Select((x, idx) => random.Next()).ToArray();
TVOHM
  • 2,740
  • 1
  • 19
  • 29