Answer the question
In order to leave comments, you need to log in
How do you replace multidimensional associative arrays in c#?
Tell me how such things are implemented in c#?
In php this is called multidimensional associative arrays, below is an example:
massiv[i] = array{
name => "",
size => "",
position => "",
text => ""
}
Answer the question
In order to leave comments, you need to log in
In my project, data about an object is pulled out from the database
public class SomeClassName {
public string Name {get;set;}
public int Size {get;set;}
public int Position {get;set;}
public string Text {get;set;}
}
List<SomeClassName> yourArray = new List<SomeClassName>();
yourArray.Add(new SomeClassName {
Name = "abc",
Size = 123,
Position = 123,
Text = "abc"
});
@AlexP11223 wrote correctly. If you do not want to use ORM, but only ADO.NET, then the fields of the record will need to be assigned to the properties of the object. The objects themselves can be stored in a list.
The simplest example:
public class Sample
{
public int Id { get; set; }
public string Name { get; set; }
}
public List<Sample> GetSamples()
{
var result = new List<Sample>();
using (var connection = new SqlConnection("connection_string"))
{
var command = connection.CreateCommand();
command.CommandText = "SELECT id, name FROM .....";
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(new Sample
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
});
}
reader.Close();
}
connection.Close();
}
return result;
}
If you choose arbitrary elements from the list and not just store them, then do not forget to get acquainted with collections like HashSet and Dictionary in addition to List. There are tasks when this simple replacement leads to a thousandfold increase in productivity.
I join the above. You can also look in the direction of Dapper, if you want everything to "map" itself.
PS
It if decided to go by pure ADO.NET.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question