Lists<> with multiple lists c# - c#

I am searching for a solution to create List<> of Lists with different data types like
List<List<int>,List<double>,List<string>> list;
or a List<> with multiple data types like
List<int,double,string> list;

In all seriousness... why?
The code which consumes these structures is going to be confusing and difficult to support. It really, really will.
If you have a custom structure to your data, create a custom object to represent that structure. Something like this:
class MyThing
{
public int Widget { get; set; }
public double Foo { get; set; }
public string Something { get; set; }
}
Use actual meaningful names for your values/types/etc. of course, because that's the entire point of doing this.
Then just have a list of those:
var things = new List<MyThing>();
As your structure continues to change and grow, you build the logic of that structure into the object itself, rather than into all of the consuming code which uses it. The code which uses it then more closely approximates the semantics of the operations it's trying to perform, rather than a dizzying assortment of syntactic operations.

May be you can do like this
public class Helper
{
public object value;
private string Type;
}
then create list
List<Helper> myList=new List<Helper>();
use like this
myList.Add(new Helper {value = 45,Type = "int"});
myList.Add(new Helper {value = 45,Type = "int"});
myList.Add(new Helper {value = "hello",Type = "string"});
and the covert according to Type.

You can make a list of lists by
List<List<int>>
To have a list with multiple data types you could use a Tuple which can take up to 8 items.
List<Tuple<string, int>>
List<Tuple<string, string, int>>
You could even go crazy and do the following
Tuple<List<int>, int, int> tuple = new Tuple<List<int>, int, int>(new List<int>(), 2, 3);

If I'm understanding you correctly, I think you need a little helper class:
public class MyInfo {
public int MyInt32 { get; set; }
public double MyDouble { get; set; }
public string MyString { get; set; }
}
Then, make a list of those: var myList = new List<MyInfo>().
You can also use a List<Tuple<int, double, string>>, but I would recommend creating your own class if the scope of usage is wider than a single class, because Tuples properties aren't very descriptive as to what they are: Item1, Item2, Item3...

Best way to solve this is to use Interface you can find solution here list of multiple data types

You can use ArrayList - the old non generic list. THis allows you to put anything in it, int, string, FooObject, bool,....

Related

What type of C# object does the following JSON map to?

I want to desalinize the following JSON into a strongly typed C# object:
{"myThings":{"One thing":"aaaaa","Two thing":"bbbbb"}}
One thing and Two thing aren't always called One thing and Two thing, these change every time, and there could be more or less myThings.
I can't figure out what type of object myThings should be, I am thinking a KeyValuePair but am unsure if Json.Net supports deserialising to a key/value pair, or if this is even correct. Can somebody confirm?
Thanks
class MyThings
{
public string OneThing { get; set; }
public string TwoThing { get; set; }
}
or
class MyThings
{
public Dictionary<string, string> Dictionary { get; set; }
}
If the number of "X things" is unknown and variable use the Dictionary<string, string>. If you are always going to have {"One thing":"aaaaa","Two thing":"bbbbb"} use the first class.
EDIT:
Given your edit, use the Dictionary<string, string>

How to make/design a class which can have different type of members at runtime in C#

I have a situation where I need a class which need to contain information about something which varies at runtime, for example:
class Info<T>
{
public T Max { get; set; }
public T Min { get; set; }
public T DefaultValue { get; set; }
public T Step { get; set; }
// Some other stuff
}
I have to store many instances of this class in a dictionary but problem is that to use dictionary I have to declare one type e.g.
Dictionary<string, Info<int>> dict = new Dictionary<string, Info<int>>();
In this case I can't add another type of info e.g. Info<double>.
I want something like , I have removed generic version in below case.
{"Price", new Info{Min=100,Max=1000,DefaultValue=200,Step=50}}
{"Adv", new Info{Min=10.50,Max=500.50,DefaultValue=20.50,Step=1.5}}
{"Answer", new Info{Min=false,Max=false,DefaultValue=false,Step=false}}
I can use Dictionary<string, Object> dict = new Dictionary<string, Object>();
but then when I get the dict item back I don't know what type is that, I need to know the type as well e.g. for Price it's int and for Adv it's double , how will I know it at runtime?
Actually I want to create a validator(I am using .Net Compact Framework 3.5/can not use any inbuilt system if it exists) for example If I have a class like below..
class Demo
{
public int Price { get; set; }
public float Adv { get; set; }
public static bool Validate(Demo d)
{
List<string> err = new List<string>();
// here I have to get Info about the Price
// from dictionary, it can be any storage
Info priceInfo = GetPriceInfo("Price");
if (d.Price < priceInfo.Min)
{
d.Price = priceInfo.Min;
err.Add("price is lower than Min Price");
}
if (d.Price > priceInfo.Max)
{
d.Price = priceInfo.Max;
err.Add("price is above than Max Price");
}
// need to do similar for all kinds of properties in the class
}
}
So idea is to store validation information at one place (in dictionary or somewhere else) and then use that info at validation time, I also would like to know if I can design the above scenario in a better way ?
Maybe there is a better way to do this , any guidelines please?
You can use a non-generic base class:
public abstract class Info {
}
public class Info<T> : Info {
}
Now all different generic types inherit from the same base type, so you can use that in the dictionary:
Dictionary<string, Info> dict = new Dictionary<string, Info>();
You can define properties and methods where the interface is not depending on the generic type in the base class, and implement them in the generic class. That way you can use them without specifying the generic type.
For methods where you need the type, you need specific code for each type. You can use the is and as operators to check for a type:
Info<int> info = dict[name] as Info<int>;
if (info != null) {
int max = info.Max;
}
You could take from Microsoft and mimic the IEnumerable interface and create a .Cast<T>? However, somebody is going to have to know about your type unless you want to get into dynamic (4.0+ only) or reflection. Both of which come with a cost. Maybe you need to rethink your design?
Keith Nicholas is right - if you want your dictionary to support multiple types, you'll need an interface, but it will need to be a generic one.
Try something like this (warning: untested code):
interface IInfo<T>
{
T Max { get; set; }
T Min { get; set; }
T DefaultValue { get; set; }
T Step { get; set; }
}
Dictionary<string, IInfo> dict = new Dictionary<string, IInfo>();
class AnswerInfo : IInfo<bool> { }
class PriceInfo : IInfo<int> { }
class AdvInfo : IInfo<double> { }
dict["Answer"] = new AnswerInfo() { Min = false, Max = false, DefaultValue = false, Step = false };
dict["Price"] = new PriceInfo() { Min = 100, Max = 1000, DefaultValue = 200, Step = 50 };
dict["Adv"] = new AdvInfo() { Min = 10.50, Max = 500.50, DefaultValue = 20.50 Step = 1.5 };
Using a Dictionary of objects (or some base class) you would have several options to get to the data (typically, involving some kind of inheritance from a common base class to work with, which has properties as outlined below).
Use an enum to denote the type, then you can have some kind of switch/case. (Easy to do, not very C#ish.)
Use something similar to a VARIANT. Variants are types that provide both information what they store and also the value stored, which can be any base type like string, int, float. (Does not really exist in C#, as you can see from the answers here Variant Type in C# .)
You can also test the type of the object at runtime to find out what kind of an object you have, and then cast it and handle its content depending on its type. (Several ways.. might have to use reflection, for a start have a look here: get object type and assign values accordingly .)
You could actually also try to abstract the operation you want to do on each object in some way, and then call that function. Something like the command pattern. (Command Pattern : How to pass parameters to a command?)
Probably many more. :)

How can I create a multi-type matrix?

I have these data :
4 pippo pluto paperino
10 marco paolo roberto
2 hello cruel world
and I'd like to store these data into a multi-type matrix myMatrix. (First field is an integer, the rest are strings).
So, if I type myMatrix[1][2] I must get paolo. How can I do it on C#?
Tuple<int, string, string, string>[]
object[][]
Dictionary<int, string[]>
You can't use arrays to store different types of objects that are not part of the same inheritance chain.
an object[][] will work, but you will not have type safety and need to cast to the target type.
You can use Tuple<int,string,string,string>[] (introduced in .NET 4.0), though your own simple type would be a more readable option.
Instead of using arrays, you could use one of the collection types - for example List<T>, where the generic type is a tuple or your custom type.
You really need to think about the usage this collection will be put into before you can select a suitable data structure.
The best way is to create a class that represent your data, with 4 fields (integer, string, string, string).
public class MyClass
{
public int Param1 { get; set; }
public string Param2 { get; set; }
public string Param3 { get; set; }
public string Param4 { get; set; }
}
Then simply use a List<MyClass> to store your data.
To get Paolo you'll need to use List[1].Param2 (also name your parameters with meaningful names).
You can use Dictionary<int, string[]> to get such effect:
Dictionary<int, string[]> myMatrix = new Dictionary<int, string[]>();
myMatrix.Add(4, new[] { "pippo", "pluto", "paperino" });
...
//get
myMatrix[4][1] == "pluto"
Dictionary<int, string[]> is the simplest way as I think.
More about Dictionary type can be found here

Architecturally speaking, how should I replace an extremely large switch statement with something more manageable?

EDIT 1: Forgot to add the nested property curve ball.
UPDATE: I have chosen #mtazva's answer as that was the preferred solution for my specific case. In retrospect, I asked a general question with a very specific example and I believe that ended up confusing everyone (or maybe just me) as to what the question was exactly. I do believe the general question has been answered as well (see the Strategy pattern answers and links). Thanks everyone!
Large switch statements obviously smell and I have seen some links on how you could do this with a dictionary that maps to functions. But I'm wondering if there is a better (or smarter way) to do this? In a way, this is a question I've always sort of had rolling around in the back of my head but never really had a good solution to.
This question stemmed from another question I asked earlier: How to select all the values of an object's property on a list of typed objects in .Net with C#
Here is an example class I'm working with (from an external source):
public class NestedGameInfoObject
{
public string NestedName { get; set; }
public int NestedIntValue { get; set; }
public decimal NestedDecimalValue { get; set; }
}
public class GameInfo
{
public int UserId { get; set; }
public int MatchesWon { get; set; }
public long BulletsFired { get; set; }
public string LastLevelVisited { get; set; }
public NestedGameInfoObject SuperCoolNestedGameInfo { get; set; }
// thousands more of these
}
Unfortunately, this is coming from an external source... imagine a HUGE data dump from Grand Theft Auto or something.
And I want to get just a small cross section of a list of these objects. Imagine we want to be able to compare you with a bunch of your friends' game info objects. An individual result for one user would look like this:
public class MyResult
{
public int UserId { get; set; } // user id from above object
public string ResultValue { get; set; } // one of the value fields from above with .ToString() executed on it
}
And an example of what I want to replace with something more manageable (believe me, I DON'T want to be maintaining this monster switch statement):
const int MATCHES_WON = 1;
const int BULLETS_FIRED = 2;
const int NESTED_INT = 3;
public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
var output = new List<MyResult>();
switch(input)
{
case MATCHES_WON:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.MatchesWon.ToString()
}).ToList<MyResult>();
break;
case BULLETS_FIRED:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.BulletsFired.ToString()
}).ToList<MyResult>();
break;
case NESTED_INT:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.SuperCoolNestedGameInfo.NestedIntValue.ToString()
}).ToList<MyResult>();
break;
// ad nauseum
}
return output;
}
So the question is are there any reasonable ways to manage this beast? What I'd really like is a dynamic way to get this info in case that initial object changes (more game info properties are added, for instance). Is there a better way to architect this so it's less clumsy?
I think your first sentence eluded to what is probably the most reasonable solution: some form of dictionary mapping values to methods.
For example, you could define a static Dictionary<int, func<GameInfo, string>>, where each value such as MATCHES_WON would be added with a corresponding lambda that extracts the appropriate value (assuming your constants, etc are defined as shown in your example):
private static Dictionary<int, Func<GameInfo, string>> valueExtractors =
new Dictionary<int, Func<GameInfo, string>>() {
{MATCHES_WON, gi => gi.MatchesWon.ToString()},
{BULLETS_FIRED, gi => gi.BulletsFired.ToString()},
//.... etc for all value extractions
};
You can then use this dictionary to extract the value in your sample method:
public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
return gameInfo.Select(gi => new MyResult()
{
UserId = gi.UserId,
ResultValue = valueExtractors[input](gi)
}).ToList<MyResult>();
}
Outside of this option, you could potentially have some sort of file/database/stored lookup with the number and the property name, then use reflection to extract the value, but that would obviously not perform as well.
I think this code is getting out of hand a bit. You're effectively using constants to index properties - and this is creating fragile code that you're looking to use some technique - such as - reflection, dictionaries, etc - to control the increased complexity.
Effectively the approach that you're using now will end up with code like this:
var results = GetMyResult(gameInfos, BULLETS_FIRED);
The alternative is to define an extension method that lets you do this:
var results = gameInfos.ToMyResults(gi => gi.BulletsFired);
This is strongly-typed, it doesn't require constants, switch statements, reflection, or anything arcane.
Just write these extension methods and you're done:
public static class GameInfoEx
{
public static IEnumerable<MyResult> ToMyResults(
this IEnumerable<GameInfo> gameInfos,
Func<GameInfo, object> selector)
{
return gameInfos.Select(gi => gi.ToMyResult(selector));
}
public static MyResult ToMyResult(
this GameInfo gameInfo,
Func<GameInfo, object> selector)
{
return new MyResult()
{
UserId = gameInfo.UserId,
ResultValue = selector(gameInfo).ToString()
};
}
}
Does that work for you?
You can use reflection for theses purposes. You can implement custom attributes, mark your properties, etc. Also, it is dynamic way to get info about your class if it changes.
If you want to manage switch code I would point you at Design Patterns book (GoF) and suggest possibly looking at patterns like Strategy and possibly Factory (thats when we talk about general case use, your case isn't very suited for Factory) and implementing them.
While switch statement still has to be left somewhere after refactoring to pattern is complete (for example, in a place where you select strategy by id), code will be much more maintanable and clear.
That said about general switch maintenance, if they become beast like, I am not sure its best solution given how similar your case statements look.
I am 100% sure you can create some method (possibly an extension method) that will be accepting desired property accessor lambda, that should be used when results are generated.
If you want your code to be more generic, I agree with the suggestion of a dictionary or some kind of lookup pattern.
You could store functions in the dictionary, but they seemly all perform the same operation - getting the value from a property. This is ripe for reflection.
I'd store all your properties in a dictionary with an enum (prefer an enum to a const) as the key, and a PropertyInfo - or, less preferred, a string which describes the name of the property - as the value. You then call the GetValue() method on the PropertyInfo object to retrieve the value from the object / class.
Here's an example where I'm mapping enum values to their 'same named' properties in a class, and then using reflection to retrieve the values out of a class.
public enum Properties
{
A,
B
}
public class Test
{
public string A { get; set; }
public int B { get; set; }
}
static void Main()
{
var test = new Test() { A = "A value", B = 100 };
var lookup = new Dictionary<Properties, System.Reflection.PropertyInfo>();
var properties = typeof(Test).GetProperties().ToList();
foreach (var property in properties)
{
Properties propertyKey;
if (Enum.TryParse(property.Name, out propertyKey))
{
lookup.Add(propertyKey, property);
}
}
Console.WriteLine("A is " + lookup[Properties.A].GetValue(test, null));
Console.WriteLine("B is " + lookup[Properties.B].GetValue(test, null));
}
You can map your const values to the names of the properties, PropertyInfo objects which relate to those properties, functions which will retrieve the property values... whatever you think suits your needs.
Of course you will need some mapping - somewhere along the way you will be depending on your input value (the const) mapping to a specific property. The method by which you can get this data might determine the best mapping structure and pattern for you.
I think the way to go is indeed some kind of mapping from one value (int) to something that is somehow a function that knows how to extract a value.
If you really want to keep it extensible, so that you can easily add some without touching the code, and possibly accessing more complex properties (ie. nested properties, do some basic computation), you may want to keep that in a separate source.
I think one way to do this is to rely on the Scripting Services, for instance evaluating a simple IronPython expression to extract a value...
For instance in a file you could store something like :
<GameStats>
<GameStat name="MatchesWon" id="1">
<Expression>
currentGameInfo.BulletsFired.ToString()
</Expression>
</GameStat>
<GameStat name="FancyStat" id="2">
<Expression>
currentGameInfo.SuperCoolNestedGameInfo.NestedIntValue.ToString()
</Expression>
</GameStat>
</GameStats>
and then, depending on the requested stat, you always end up retrieving the general GameInfos. You can them have some kind of foreach loop with :
foreach( var gameInfo in gameInfos){
var currentGameInfo = gameInfo
//evaluate the expression for this currentGameInfo
return yield resultOfEvaluation
}
See http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml for examples on how to embed IronPython Scripting in a .NET application.
NOTE: when working with this kind of stuff, there are several things you must really be careful about:
this potentially allows someone to inject code in your application ...
you should measure the performance impact of Dynamic evaluation in here
I don't have a solution to your switch problem off the top of my head, but you could certainly reduce the code by using a class that can automatically map all the fields you need. Check out http://automapper.org/.
I would not have written the GetMyResult method in the first place. All it is doing is transforming GameInfo sequence into MyResult sequence. Doing it with Linq would be easier and more expressive.
Instead of calling
var myResultSequence = GetMyResult(gameInfo, MatchesWon);
I would simply call
var myResultSequence = gameInfo.Select(x => new MyResult() {
UserId = x.UserId,
ResultValue = x.MatchesWon.ToString()
});
To make it more succinct you can pass the UserId and ResultValue in constructor
var myResultSequence =
gameInfo.Select(x => new MyResult(x.UserId, x.MatchesWon.ToString()));
Refactor only if you see the selects getting duplicated too much.
This is one possible way without using reflection:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class GameInfo
{
public int UserId { get; set; }
public int MatchesWon { get; set; }
public long BulletsFired { get; set; }
public string LastLevelVisited { get; set; }
// thousands more of these
}
public class MyResult
{
public int UserId { get; set; } // user id from above object
public string ResultValue { get; set; } // one of the value fields from above with .ToString() executed on it
}
public enum DataType
{
MatchesWon = 1,
BulletsFired = 2,
// add more as needed
}
class Program
{
private static Dictionary<DataType, Func<GameInfo, object>> getDataFuncs
= new Dictionary<DataType, Func<GameInfo, object>>
{
{ DataType.MatchesWon, info => info.MatchesWon },
{ DataType.BulletsFired, info => info.BulletsFired },
// add more as needed
};
public static IEnumerable<MyResult> GetMyResult(GameInfo[] gameInfos, DataType input)
{
var getDataFunc = getDataFuncs[input];
return gameInfos.Select(info => new MyResult()
{
UserId = info.UserId,
ResultValue = getDataFunc(info).ToString()
});
}
static void Main(string[] args)
{
var testData = new GameInfo[] {
new GameInfo { UserId="a", BulletsFired = 99, MatchesWon = 2 },
new GameInfo { UserId="b", BulletsFired = 0, MatchesWon = 0 },
};
// you can now easily select whatever data you need, in a type-safe manner
var dataToGet = DataType.MatchesWon;
var results = GetMyResult(testData, dataToGet);
}
}
}
Purely on the question of large switch statements, it is notable that there are 2 variants of the Cyclomatic Complexity metric in common use. The "original" counts each case statement as a branch and so it increments the complexity metric by 1 - which results in a very high value caused by many switches. The "variant" counts the switch statement as a single branch - this is effectively considering it as a sequence of non-branching statements, which is more in keeping with the "understandability" goal of controlling complexity.

Single sorted c# list with different types?

Yes, I'm new to c#! :) i'm using .Net4 VS2010.
I have Three classes each one is used to build a list of objects of that type. All three inherit form a base class.
I want to combine the resulting three lists in to one and sort them on one of the base class elements.
Can this be done with lists of different types?
Simplified Example:
Each list is created
public List<TestOne> TestOne list;
public List<TestTwo> TestTwoList;
public List<object> BothLists;
Code to fill TestOne and TestTwo…
What/How do I combine both TestOne and TestTwo into BothLists and sort them on SeqNumber???
public class BaseClassTest
{
public string Loc { get; set; } // loc
// sequence number to order by will be assigned in the resulting class
public int SeqNumber { get; set; }
}
public class TestOne : BaseClassTest
{
public int Number { get; set; }
}
public class TestTwo : BaseClassTest
{
public string CatName { get; set; }
}
You should be able to do:
List<BaseClassTest> sorted = TestOneList.Cast<BaseClassTest>()
.Union(TestTwoList.Cast<BaseClassTest>())
.OrderBy(item => item.SeqNumber)
.ToList();
This will do your sort + union all at once.
"BothLists" should be a List<BaseClassTest>. That should let you sort on base class properties, using .OrderBy(x => x.SequenceNumber).
EDITED TO ADD:
Following up from comments, this should work to combine the lists:
BothLists = TestOneList.OfType<BaseClassList>().Concat().(TestTwoList.OfType<BaseClassList>()).ToList();
Generally, .OfType<>() is preferable to .Cast<>() because it simply filters based on type, rather than forcing a cast.
Given your example this is quite easy:
public List<BaseClassTest> BothLists;
Then you can sort:
BothLists.Sort((a, b) => a.SeqNumber.CompareTo(b.SeqNumber));
If a class inherits from a base, it counts as the base class for most operations. Thus:
List<BaseClassTest> AllTests;
Should get you what you need.
Make TestOne and TestTwo implement the IComparable interface. See here. Then you can combine the lists into an ArrayList and use the Sort method.
List<BaseClassTest> list3 = new List<BaseClassTest>();
list3.AddRange(list1);
list3.AddRange(list2);
var sorted = list3.OrderBy(e => e.SequenceNum).ToList(); //if you really need a list back
This is all assuming you have Linq available. Of course if you do a Union statement might work also.

Categories