I am currently studying computing at UCLAN and I have to write a program using Spec# and I need a 2 dimensial jagged array which can not be null.
I know for a normal array I can declare it like this
T![]!
but when i want to declare it for a jagged array I should write it somehow like this
T![]![]
This works pretty well but when I want to init it:
T![]![] = new T![365]![]
it throws an error and I just can't find how to get this fixed.
I did not find out how I can init the array correctly but I found a workaround.
T![]![]! = (T![]![]) new T![][];
This is how it works!
Related
So, I am stuck with one weird problem.
To give you idea what I want to achieve, but failed miserably:
int[] testArray = new int[100];
typeof(int[])
.GetProperty("Length", BindingFlags.Instance | BindingFlags.Public)
.SetValue(testArray, 50);
Yeah, that is, I want to modify C# arrays Length property (only make it less than actual size), without making new array and copying contents - to avoid GC etc etc...
Background:
In Unity, there is this function:
void SetIndices(int[] indices, MeshTopology topology, int submesh, bool calculateBounds);
As you see, SetIndices doesn't accept "start" and "end" or at least "length" as a parameter. This is why I want to modify Length of my indices array to trick this function.
Any ideas?
You can't resize an array in C#. That's the hard truth.
In fact, the Array.Resize method creates a new array and copies the values of the previous array in the new one.
I have a question about storing lots of arrays into a list.
First, I initialize the array and list:
int[] arr = new int[9];
List<int[]> forkarr = new List<int[]>();
then I run through a lot of for loops and modify the array each time in order to produce all possible tic tac toe variations. While looping through all those variations, if the array is a possible board then I print it, and additionally if the array meets certain criteria for being a 'fork', then I will add it to the list like this:
if (ForkCheck.fork(arr)) { forkarr.Add(arr); Console.WriteLine("It's a Fork!");}
which also prints that message letting you know that particular array is a fork.
Now, when I am printing all of the arrays, the ones that are forks are printed properly and labeled as such.
However, when I go to print out all of the int[] elements of my list forkarr like so:
foreach (int[] arry in forkarr)
{
PrintGame.print(arry);//this is my method that prints the array
Console.WriteLine();
}
for some reason each array becomes an identical:
222
222
222
(I'm using 2 as 'X' and 1 as 'O' and 0 as 'empty' btw)
And it just prints that out over and over for all the forks.
So, something is going wrong when I am adding each modification of the array as a new element in the list I think, but I'm not sure why.
Thanks for any help.
Because you are modifying the same array and adding it to the list. Each time you done with one array array you need to create a new instance like this:
arr = new int[9];
This will create a new reference which will be independent from other arrays. And modifying it's elements wont affect the others.
For more information about value vs reference types you can refer to the question below:
What is the difference between a reference type and value type in c#?
I'm trying to figure out if there's a way to do something in C# that's fairly easy to do in C++. Specifically, if I have an array of data, I can create a pointer into that data to access a subsection more conveniently.
For example, if I have:
unsigned char buffer[1000];
and I determine that there's a string at positions 102 to 110 within that array that I need to manipulate a lot, I can do this:
unsigned char *strPtr = &buffer[102];
char firstChar = strPtr[0];
This saves me from having to add "102" to each array index in subsequent operations.
While I recognize the possibility of unsafe situations when you do something like this, I'm wondering if there is a moral equivalent in C# that would let me create a new reference to a subset of an existing array.
Something like:
byte [8] newArray = &buffer[102];
That example doesn't actually work or I wouldn't be posting this, but I think it gets the idea of what I want to accomplish across.
There's the ArraySegment<T> class which can be used as a wrapper to access segments of an array. Just provide the array, an offset and the length of the segment and you could use it as if it were the actual array. You'll get bounds checking and other niceties of using arrays.
var buffer = new byte[1000];
var newArray = new ArraySegment<byte>(buffer, 102, 8) as IList<byte> // we have an "array" of byte[8]
var firstChar = newArray[0];
There is a proposal however to introduce array slicing. As like the ArraySegment, slices allow you to create views into arrays (without making copies) and can be used in place of actual arrays. Hopefully it will make it into a (near) future C# version.
Is it possible in c# to initialize an array in, for example, subindex 1?
I'm working with Office interop, and every property is an object array that starts in 1 (I assume it was originally programed in VB.NET), and you cannot modify it, you have to set the entire array for it to accept the changes.
As a workaround I am cloning the original array, modifying that one, and setting it as a whole when I'm done.
But, I was wondering if it was possible to create a new non-zero based array
It is possible to do as you request see the code below.
// Construct an array containing ints that has a length of 10 and a lower bound of 1
Array lowerBoundArray = Array.CreateInstance(typeof(int), new int[1] { 10 }, new int[1] { 1 });
// insert 1 into position 1
lowerBoundArray.SetValue(1, 1);
//insert 2 into position 2
lowerBoundArray.SetValue(2, 2);
// IndexOutOfRangeException the lower bound of the array
// is 1 and we are attempting to write into 0
lowerBoundArray.SetValue(1, 0);
You can use Array.CreateInstance.
See Array Types in .NET
Not simply. But you can certainly write your own class. It would have an array as a private variable, and the user would think his array starts at 1, but really it starts at zero and you're subtracting 1 from all of his array accesses.
You can write your own array class
I don't think if it's possible to modify the starting index of arrays.
I would create my own array using generics and handle it inside.
Just keep of const int named 'offset' with a value of one, and always add that to your subscripts in your code.
I don't think you can create non-zero based arrays in C#, but you could easily write a wrapper class of your own around the built in data structures.This wrapper class would hold a private instance of the array type you required; overloading the [] indexing operator is not allowed, but you can add an indexer to a class to make it behave like an indexable array, see here. The index function you write could then add (or subtract) 1, to all index's passed in.
You could then use your object as follows, and it would behave correctly:
myArrayObject[1]; //would return the zeroth element.
In VB6 you could change the array to start with 0 or 1, so I think VBScript can do the same. For C#, it's not possible but you can simply add NULL value in the first [0] and start real value at [1]. Of course, this is a little dangerous...
Is it possible in c# to initialize an array in, for example, subindex 1?
I'm working with Office interop, and every property is an object array that starts in 1 (I assume it was originally programed in VB.NET), and you cannot modify it, you have to set the entire array for it to accept the changes.
As a workaround I am cloning the original array, modifying that one, and setting it as a whole when I'm done.
But, I was wondering if it was possible to create a new non-zero based array
It is possible to do as you request see the code below.
// Construct an array containing ints that has a length of 10 and a lower bound of 1
Array lowerBoundArray = Array.CreateInstance(typeof(int), new int[1] { 10 }, new int[1] { 1 });
// insert 1 into position 1
lowerBoundArray.SetValue(1, 1);
//insert 2 into position 2
lowerBoundArray.SetValue(2, 2);
// IndexOutOfRangeException the lower bound of the array
// is 1 and we are attempting to write into 0
lowerBoundArray.SetValue(1, 0);
You can use Array.CreateInstance.
See Array Types in .NET
Not simply. But you can certainly write your own class. It would have an array as a private variable, and the user would think his array starts at 1, but really it starts at zero and you're subtracting 1 from all of his array accesses.
You can write your own array class
I don't think if it's possible to modify the starting index of arrays.
I would create my own array using generics and handle it inside.
Just keep of const int named 'offset' with a value of one, and always add that to your subscripts in your code.
I don't think you can create non-zero based arrays in C#, but you could easily write a wrapper class of your own around the built in data structures.This wrapper class would hold a private instance of the array type you required; overloading the [] indexing operator is not allowed, but you can add an indexer to a class to make it behave like an indexable array, see here. The index function you write could then add (or subtract) 1, to all index's passed in.
You could then use your object as follows, and it would behave correctly:
myArrayObject[1]; //would return the zeroth element.
In VB6 you could change the array to start with 0 or 1, so I think VBScript can do the same. For C#, it's not possible but you can simply add NULL value in the first [0] and start real value at [1]. Of course, this is a little dangerous...