I'm fine with understanding lines of code and the examples in my books but when I mess around on my own it all goes wrong. I can't seem to grasp the philosophy or design principles of the language when I want to assign values to variables and constants or set them to the values of other variables and constants.
It seems you can set numbers using maths a = fred * 3; and a few other things but in other cases you can't.
For instance it seems you can define a new array = new int[3] {1, 2, 3} but you can't set an array by going arr = {1, 2, 3} in the main code. In fact you can't even copy the value of one array to another by going arr1 = arr2 , it seems to treat it as a pointer and you have to use something like .Copy(arr1, arr2)
Then on the other hand it turns out you can go myButton.BackColor = Color.LightGray; which is exactly the sort of thing I would expect to be used as myButton.BackColor(LightGray);
There seem to be load of cases of this. Now each case is fine but how are you meant to know which to use? Is it just a case of getting to know them all or is there actually some kind of syntax philosophy that I'm missing?
Sam HobbsPosted Oct 28, 2010, 4:58 PM
I don't know what processor you are familiar with the assembly language for, but hopefully it is one that has a stack. If it is an IBM mainframe type of processor then you might not be familiar with the concept of a stack. I will assume you are familiar with processor stacks, such as the 8086 and Pentium processor's SP register. I also assume you understand the concept of heaps.
One thing that can be confusing about C# is that it has reference types and value types. Just that one subject can cause immense confusion. Supposedly reference types exist in the heap and value types in the stack, but I am still not entirely clear about that. Suposedly reference types are classes and value types are structs, but there seem to be some confusing inconsistencies about that.
If you want more help with more specific answers, create new threads with more specific questions, such as how to assign values to arrays and such.
JurePosted Oct 28, 2010, 7:22 AM
1) arrays are initialized like this:
int[] array = new int[] { 1, 2, 3 };
int[] array2 = (int[])array.Clone();
You don't have to specify number of elements, but you can if you need to.
The "(int[])" part is called a cast. Casting means converting a value (not an object itself) from one type to another. Clone() method returns an object of type "object", so it's necessary to explicitly convert the returned value to int array.
In C# a lot of things are acting like objects (strings for example) and variables are references to those objects. For example, if you want to change a string:
string text = "this is a sample";
text = text.Insert(0, "+");
text.Insert simply returns a modified string, so you have to assign the returned value back to the variable. I know it might seem kind of pointless at first, so consider this:
string text = "this is a sample";
string another_one = text.Insert(0, "+");
If Insert method would change the original string, then you'd need an extra step, you'd have to assign text to the another_one first, and then call Insert on another_one. So the sacrifice of having to write string = string.Insert instead of just string.Insert is very small in comparison.
2) BackColor is a "property". Properties are functions in their core, but are used the same way as fields, which makes syntax much more readable (assign or pass the value - which one is more logical).
I suggest you try out Visual Studio, if you're not using it already. You can download Visual Studio Express 2010 for free.