Quantcast
Channel: Best way to "push" into C# array - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Answer by Mondonno for Best way to "push" into C# array

$
0
0

There is another way to do this, which is fitting perfectly for extension class.

public static void Push<T>(ref T[] table, object value){    Array.Resize(ref table, table.Length + 1);    table.SetValue(value, table.Length - 1);}

What is going on here, is that we are resizing and then setting value for the new element created by Array.Resize(...) method.

Here is a snippet with example usage.


Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>