Okay, I'll caveat this question with two things. One, I'm not as smart as you (I'm really not). Second, I may have found an answer to my own question, but want to verify.
I'm attempting to declare multiple arrays at the same time. It looks something like this. The first is just declaring a single array, while the second tries to declare both profit_Normal and profit_High in the same line:
double [] tradeType_Active = new double [15];
double [] profit_Normal, profit_High = new double [5];
Can I do this? I currently use this syntax for declaring non-array values with commas, like this:
double
BUpper,
BUpper_Prev,
BUpper_Prev2;
Please let me know when you have a moment.
Your line of code
double[] profit_Normal, profit_High = new double[5];
does declare multiple double[]. What it doesn't to is to initialize all of them. It initializes only the second one.
If you have the following line:
double[] a, b, c, d = new double[5];
what happens is that you are declaring 4 references of arrays of double. For each array you declare you must initialize it - which you are doing only for the last.
To initialize multiple arrays you need to actually initialize them:
double[] profit_Normal = new double[5], profit_High = new double[5];
The difference between the arrays case and this double BUpper, BUpper_Prev, BUpper_Prev2; is that arrays are reference types that their default value is null and must be initialized, whereas doulbe's default value is 0.
Yes, this is absolutely allowed, as long as you keep [] in the declaration to indicate that you are declaring an array:
double[] a = new double[4], b = new double[5];
The double[] part is the type of variables being declared. In your case, that's an array of double.
Note that the only difference between the above and your second declaration is that you did not initialize the profit_Normal variable.
You can use the same syntax you currently use, but in order to instantiate each one as well as declaring it, it would look like this, with = new double[5] after each one:
double[]
profit_Normal = new double[5],
profit_High = new double[5];
Related
Let's say I have this code (this is an example):
int test = 5;
List<int> testlist = new List<int>();
testlist.Add(test);
test = 7;
Console.WriteLine(testlist[0]);
This gives output: 5. What I want is 7.
How can I make it so that element in list is the same as the original one? In other words I want it to also change element in list when I change the original value and the other way around. In C++ I would make a Vector of pointers. AFAIK there are no pointer types in C# so what is the workaround?
Use like this,
int test = 5;
List<int> testlist = new List<int>();
test = 7; // this line need to come up
testlist.Add(test); // This line comes after test = 7;
Console.WriteLine(testlist[0]);
Now you can get output 7.
The problem you have is that you're storing ints, wich are value types. What you can do, is create a wrapper class that has one public int. Instead of making a list of ints, you make a list of your wrapper class. That way when you change the value of the int, the changes will show in your list.
I have declaration as follow:
IList<int[]> populacja = new List<int[]>();
but I want to declare also a constant size of int table. So I want something like this
IList<int[2]> populacja = new List<int[2]>();
How to do that? What is a good solution for making list of int table ?
If it's important for you to ensure that populacja is always int[2], you can wrap it in a class, then make a list of that class. Built in options include Tuple, for example:
IList<Tuple<int,int>> populacja = new List<Tuple<int,int>>();
Just an FYI.
The reason you cant declare int[2] in IList is because it is expecting a type. In this case, the type is an array of int (int[]). The T doesn't care about the length or anything like that. If you did want to use int[] you will need to do something like this --
IList<int[]> populacja = new List<int[]>();
populacja.Add(new int[2]); // empty int array of size 2
populacja.Add(new [] { 3211,3212 }); // non-empty int array of size 2
Every time you add a new array of int, you will need to explicitly instantiate it with a size of 2 since there is no constraint.
I found something that bothers me, it would be great if one of you could explain it to me. Maybe this question was asked before, but I'm really out of ideas how to name it. Here's the problem:
array1 = {1,2,3,4,5};
array2 = array1;
array1[0] = 10
Console.WriteLine(array2[0]); // - it will say "10" besides of "1"
and when I use normal variables instead of arrys, like, ie:
int a = 5;
int b = a;
a = 10;
Console.WriteLine(b); // - this one will have value "5" instead of 10.
I know how to copy arrys with values, I'm just curious why it works like that.
The variable array1 does not have a value like 1 or 'c', instead it holds an address that points to the place in memory where the data is stored.
So array2 = array1 was just giving array2 the same address as array1... they both point to the same place.
If you want to allocate a new chunk of memory, you'd have to declare a new array: int[] array2 = new int[5]; This reserves 5*32 bits in memory for your new array and gives array2 the address of the first bit.
Actually when you say array2 = array1; then it means that you have two variables that refer to the same array.
If you want to copy then you can try this:
Array.Copy(array1, array2, array2.Length);
To explain it more actually you are not copying the array since array is a reference type when you say array2 = array1; then what it actually does is that it assigns another variable to point to the same array.And you only have one instance of it. So even if you change the data using one of the variable then the changes will not be reflected in your result and hence the issue.
What you have here is difference between value and references type. Please read this article: http://jonskeet.uk/csharp/references.html
It should explain you how it works :)
I'm trying to make some test data with which to test some functionality of my code. To this end, I need a double[][]. I am trying to do with a function that takes a double[][] as an input parameter and copying onto it a local variable containing the test data. However, I get an error that I don't quite understand (I'm sure it's a very basic error, which is why I'm unable to Google it), understanding/fixing which I'd appreciate any help.
private void makeData(double[][] patterns)
{
double[][] data = new double[2][];
// exists so that I can change `data` easily, without having to change the core functionality of copying it over to `patterns`
data[0] = {1.0,8.0}; // error!
// copy over everything from data into patterns
}
The line marked in the above code is giving me the error Only assignment, call, increment, decrement, and new objects can be used as a statement. To this, my reaction is "Isn't data[0] = {1.0,8.0}; an assignment?
I'm fairly confused, so I'd appreciate any help
You want to do
data[0] = new[] {1.0, 8.0};
The curly brace initializers are only valid if you're creating an object/array. They don't work by themselves.
You can specify the type specifically:
data[0] = new double[] {1.0, 8.0};
But you don't have to if the compiler can infer the right type (which, in your case, it can).
Just replace:
data[0] = {1.0,8.0};
by:
data[0] = new double[] { 1.0, 8.0 };
The compiler has to know explicitly what to assign to data[0]. It doesn't infer it from the type of data[0].
You should to initialize your subarray first.
double[][] data = new double[2][];
data[0] = new double[] {1.0f, 8.0f};
Without initialization how is it possible to assign values to arrays?
string[] s={"all","in","all"};
I mean why did not the compile show error?.Normally we need to
initialize ,before assign values.
It's just syntactic sugar.
This:
string[] s = {"all","in","all"};
is compiled to the same code as:
string[] tmp = new string[3];
tmp[0] = "all";
tmp[1] = "in";
tmp[2] = "all";
string[] s = tmp;
Note that the array reference is not assigned to s until all the elements have been assigned. That isn't important in this particular case where we're declaring a new variable, but it would make a different in this situation:
string[] s = { "first", "second" };
s = new string[] { s[1], s[0] };
The same is true for object and collection initializers - the variable is only assigned at the end.
It is possible to declare an array variable without initialization.
Check this out
http://msdn.microsoft.com/en-us/library/0a7fscd0%28VS.71%29.aspx
You aren't "assigning a value to array". You are initializing a variable of type "reference to array". The value with which you initialize it is a reference to an array which was created by the use of short array initializer syntax {...}. While it is only permissible in initializer of variables of array type, it is exactly equivalent to new T[] { ... }, where T is deduced from type of variable.
I think you want to know why
string[] s={"all","in","all"};
works when you would expect to be required to initialize the array first like this :
string[] s = new string[];
or
string[] s = new string[] {"all","in","all"};
The answer is just compiler magic. The compiler knows based on the initialization how big to make the array so it just does it behind the scenes for you. Much like the var keyword, the point is to limit the amount of redundant information you're required to type.
The {"all","in","all"} part is the initialization. the new string[] part can be omitted because the curly braces and string are short hand notation. Take a look at MSDN on Single Dimension Arrays.
string[] s = new string[] { "all","in","all"};
and its shorthand version
string[] s = {"all","in","all"};
are the same thing. See MSDN (Initializing Arrays section) for more detail.
You don't need the new string[] part in C#3 or higher - this works fine
string[] s = { "all","in","all"};
It's just a case of the compiler being a bit smarter and working out what you mean - the back end IL will be the same.
You can do so simply because it is allowed, doing so in two steps is not necessary so this is the shorthand. Consider it sugar.