I want to assign value of tax_id, found a solution in php but i want to implement in c#, Please have a look
this is php code,'tax_id'=>array(array(6,0,array(13)))
can anyone convert this code snippets in c#, thanks
Perhaps you wan't a 3D Jagged array:
var jagged3D= new int[][][]
{
new int[][]{
new int[]{6},
new int[]{0},
new int[]{13}
}
};
Or a 2DJagged of a custom object
var jaggedCustom = new Custom[][] {
new Custom[]{
new Custom{
A =3,
B =0,
C = new int[]{13}
}
}
};
But all element of an array has the same type. You can learn more about Single-Dimensional Arrays, Multidimensional Arrays, Jagged Arrays with Msdn documentation. Using the left panel to navigate.
Related
i used
double [,] marks=new double[26,5]
int[] function = object.verify(marks)
public void verifymarks(double[][] marks)
error i get is cannot convert from double[,] to double[][]
i tried to search in the internet but couldnot find any solution. I have just began to use c#. Please Help. THankx in advance
double[][] is jagged. It's literally an array of arrays.
double[,] is multidimensional. It gets special compiler treatment.
They have different memory layouts. double[][] is an array which holds references to the other arrays, while double[,] is laid out single-dimensionally for easier use.
Another difference is what data they can hold. A jagged array can be like this:
1231243245345345345345
23423423423423
2342342343r234234234234234234
23423
While a multidimensional array has to be a perfect table, like this:
34534534534534
34534534534533
34534534534534
34534534534545
A way to get around it would be to use nullable ints.
Now, to solve your problem:
Change your method signature to public void verifymarks(double[,] marks) and in the method change anything that uses marks[x][y] to marks[x,y].
double[][] is called a Jagged array. It is an array of array (the same way you could create a list of list). With this structure you can specify a different size for each sub-array.
For exemple:
double[][] jaggedArray = new double[2][];
jaggedArray[0] = new double[5];
jaggedArray[1] = new double[151];
The other writing double[,] is a 2D array (Multidimensional array in general). You can see it as a table.
A very handy feature of multidimensional array is the initialization:
string[,] test = new string[3, 2] { { "one", "two" },
{ "three", "four" },
{ "five", "six" } };
Here's a visual, from LINQPad's Dump() function.
The first is the double[,], which creates a matrix. The second is double[][], which is just an array of arrays.
When creating webservices, in c#, I have found it very useful to pass back jagged arrays, i.e. string[][]
I also found a neat trick to build these in a simple way in my code, which was to create a List and convert it by doing a ToArray() call.
e.g.
public string[][] myws() {
List<string[]> output = new List<string[]>();
return output.ToArray();
}
I would like to be able to employ a similar solution, but I can't think how to do something similar with a 3 level jagged array or string[][][], without resorting to loops and such.
Regards
Martin
You can get there by doing a Select() which converts each inner List<string> to an array using ToArray(), and then converting those results using ToArray():
var x = new List<List<string[]>>();
string[][][] y = x.Select(a => a.ToArray()).ToArray();
And so on for as many levels deep as you'd want to go.
I am planning to rewrite my Python Tile Engine in C#. It uses a list of all the game objects and renders them on the screen. My problem is that unlike in Python where you can add almost anything to an array (e.g x = ["jj" , 1, 2.3, 'G', foo]) you can add only one type of objects
in a C# array (int[] x = {1,2,3};) . Are there any dynamic arrays (similar to the ArrayList() class) or something which allows you to pack different types into a single array? because all the game objects are individual classes.
Very simple—create an array of Object class and assign anything to the array.
Object[] ArrayOfObjects = new Object[] {1,"3"}
you can use an object array. strings, int, bool, and classes are all considered objects, but do realize that each object doesn't preserve what it once was, so you need to know that an object is actually a string, or a certain class. Then you can just cast the object into that class/data type.
Example:
List<object> stuff = new List<object>();
stuff.add("test");
stuff.add(35);
Console.WriteLine((string)stuff[0]);
Console.WriteLine((int)stuff[1]);
Though, C# is a strongly typed language, so I would recommend you embrace the language's differences. Maybe you should look at how you can refactor your engine to use strong typing, or look into other means to share the different classes, etc. I personally love the way C# does this, saves me a lot of time from having to worry about data types, etc. because C# will throw any casting (changing one data type to another) errors I have in my code before runtime.
Also, encase you didn't know, xna is C#'s game framework (didn't have it as a tag, so I assume you aren't using it).
You can write an abstract base class called GameObject, and make all gameObject Inherit it.
Edit:
public abstract class GameObject
{
public GameObject();
}
public class TileStuff : GameObject
{
public TileStuff()
{
}
}
public class MoreTileStuff : GameObject
{
public MoreTileStuff()
{
}
}
public class Game
{
static void Main(string[] args)
{
GameObject[] arr = new GameObject[2];
arr[0] = new TileStuff();
arr[1] = new MoreTileStuff();
}
}
C# has an ArrayList that allows you to mix types within an array, or you can use an object array, object[]:
var sr = new ArrayList() { 1, 2, "foo", 3.0 };
var sr2 = new object[] { 1, 2, "foo", 3.0 };
In c# we use an object[] array to store different types of data in each element location.
object[] array1 = new object[5];
//
// - Put an empty object in the object array.
// - Put various object types in the array.
// - Put string literal in the array.
// - Put an integer constant in the array.
// - Put the null literal in the array.
//
array1[0] = new object();
array1[1] = new StringBuilder("Initialized");
array1[2] = "String literal";
array1[3] = 3;
array1[4] = null;
You can use object[] (an object array), but it would be more flexible to use List<object>. It satisfies your requirement that any kind of object can be added to it, and like an array, it can be accessed through a numeric index.
The advantage of using a List is you don't need to know how items it will hold when you create it. It can grow and shrink dynamically. Also, it has a richer API for manipulating the items it contains.
Here is how you can do it
Use List<object> (as everything is derived from object in C#):
var list = new List<object>();
list.Add(123);
list.Add("Hello World");
Also dynamic might work for you (and your python background)
var list = new List<dynamic>();
list.Add(123);
list.Add(new
{
Name = "Lorem Ipsum"
});
If you wan't to use dynamic you really need to know what you're doing. Please read this MSDN article before you start.
But do you need it?
C# is a strongly-typed and very solid programming language. It is very flexible and great for building apps using object-oriented and functional paradigms. What you want to do may be acceptable for python, but looks pretty bad on C#. My recommendation is: use object oriented programming and try to build model for your problem. Never mix types together like you tried. One list is for a single data-type. Would you like to describe your problem in depth so that we can suggest you a better solution?
In C# 4 and later you can also use dynamic type.
dynamic[] inputArray = new dynamic[] { 0, 1, 2, "as", 0.2, 4, "t" };
Official docu
You can mix specific types doing the following:
(string, int)[] Cats = { ("Tom", 20), ("Fluffy", 30), ("Harry", 40), ("Fur Ball", 40) };
foreach (var cat in Cats)
{
Console.WriteLine(string.Join(", ", cat));
}
You have to declare your array with the datatype object:
object[] myArray = { };
myArray[0] = false;
myArray[1] = 1;
myArray[2] = "test";
You can use an array of object class and all it possible to add different types of object in array.
object[] array = new object[3];
array[0] = 1;
array[1] = "string";
array[3] = 183.54;
I need to copy an array to a linked list OR transform the array in a linked list.
How this can be done in .NET (C# or VB)?
Thanks
Depending on what version we're taking about here, you can :
LinkedList<YourObjectType> ListOfObjects=new LinkedList<YourObjectType>(YourObjectArray);
To go to LinkedList from array:
var array = GetMyArray();
LinkedList<MyType> list = new LinkedList<MyType>(array);
To go to array from LinkedList:
var array = list.ToArray();
In .Net v2.0 or above:
Object[] myArray = new Object[] { 1, "Hello", 2, 3.0 };
LinkedList<Object> linkedList = new LinkedList<Object>(myArray);
You can replace Object with the type of element that the array actually holds.
I need to know how to initialize array of arrays in C#..
I know that there exist multidimensional array, but I think I do not need that in my case!
I tried this code.. but could not know how to initialize with initializer list..
double[][] a=new double[2][];// ={{1,2},{3,4}};
Thank you
PS: If you wonder why I use it: I need data structure that when I call obj[0] it returns an array.. I know it is strange..
Thanks
Afaik, the most simple and keystroke effective way is this to initialize a jagged array is:
double[][] x = new []{new[]{1d, 2d}, new[]{3d, 4.3d}};
Edit:
Actually this works too:
double[][] x = {new[]{1d, 2d}, new[]{3d, 4.3d}};
This should work:
double[][] a = new double[][]
{
new double[] {1.0d, 2.0d},
new double[] {3.0d, 4.0d}
};
As you have an array of arrays, you have to create the array objects inside it also:
double[][] a = new double[][] {
new double[] { 1, 2 },
new double[] { 3, 4 }
};
double[][] a = new double[][] {
new double[] {1.0, 1.0},
new double[] {1.0, 1.0}
};
I don't know if I'm right about this, but I have been using socalled Structures in VB.net, and wondering how this concept is seen in C#. It is relevant to this question in this way:
' The declaration part
Public Structure driveInfo
Public type As String
Public size As Long
End Structure
Public Structure systemInfo
Public cPU As String
Public memory As Long
Public diskDrives() As driveInfo
Public purchaseDate As Date
End Structure
' this is the implementation part
Dim allSystems(100) As systemInfo
ReDim allSystems(1).diskDrives(3)
allSystems(1).diskDrives(0).type = "Floppy"
See how elegant all this is, and far better to access than jagged arrays. How can all this be done in C# (structs maybe?)