开发者

Possible ways of representing data in memory (.net)

开发者 https://www.devze.com 2023-02-01 02:38 出处:网络
What are the possible ways of representing data in memory in .Net (or in general)? It would be great if data could be sorted and looked up by key (or multiple keys).

What are the possible ways of representing data in memory in .Net (or in general)?

It would be great if data could be sorted and looked up by key (or multiple keys). We are thinking to use collections, arr开发者_StackOverfloways, list of collections/arrays. One object would be in several collections (one sorted asc, other desc, etc.).

Maybe this is not a good idea, and we would like to hear some other possible solutions.

Thank you


What are the possible ways of representing data in memory in .Net (or in general)?

In .NET you have two types: value types and reference types which could be stored differently in memory. Also it's the responsibility of the CLR to decide how to represent data in memory so that the developer shouldn't worry about it.

It would be great if data could be sorted and looked up by key (or multiple keys)

You may take a look at the Dictionary<TKey, TValue> class. You also have static arrays, dynamic lists, ... (this list is enormous)


That's an extremely broad question. See: data structures

The .NET library includes classes which represent many commonly used data structures.


As Mud says, this is a pretty general question so you may need to be a bit more specific for a precise answer for what you want.

In general .NET provides all the 'normal' collection types (Dictionary, List, etc.) like all other good languages. In addition, .NET provides LINQ which is brilliant for sorting and querying collections. e.g.

var array = new string[] {"banana", "pineapple", "apple", "cherry"};

// basic sorting
var sortedArray = array.OrderBy(a => a).;

foreach (var s in sortedArray)
{
    Console.WriteLine(s);
}

// filtering
var filteredArray = array.Where(a => a.Contains("apple"));

foreach (var f in filteredArray)
{
    Console.WriteLine(f);
}

LINQ will work on any sort of collection. To use it you'll need .NET 3.5 or later and you must add using System.Linq to access the necessary extension methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消