Concatenate elements of a list in c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wanted to try some examples in C# using collections and generics but I am stuck at this example.
This is my code in c#,
public List<string> CurrentCount(int week, int year)
{
List<string> lst = new List<string>();
lst.Add("current:");
lst.Add("10");
lst.Add("target:");
lst.Add("15");
return lst;
}
It gives me result like this :
["current:","10","target:","15"]
But I want it like this :
["current": 10,"target": 15] or
["current": "10","target": "15"]
Any ideas will be helpful.
Thanks.

You want a Dictionary, not a List.
var dict = new Dictionary<string, int>();
dict.Add("current", 10);
dict.Add("target", 15);

Related

Search Array and get substring result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.
Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));

C# switch case Array contains string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to look up if a string in contained in an Array, with a switch case?
string text = "blalbac";
string arr[] = {"a","b","c"};
switch (text)
{
case arr.Contains(filename):
//do..
break;
}
I’m not sure what you’re trying to do. Do you want something like
foreach(string item in arr)
{
if(text.Contains(item))
{
...
}
}

assign string from list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've created a list with a lot of options, let's say for every character a different color.
string A = "#FFB97FC9";
string B = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(answer1);
now let's say that Options.Answer1 = A.
I want answerRectangle to have the color from the code #FFB97FC9
How can I achieve this?
NOTE: I want to have answer1 = #FFB97FC9
Use a dictionary instead of variables:
var colors = new Dictionary<string, string>();
colors["A"] = "#FFB97FC9";
colors["B"] = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(colors[answer1]);
Why not create a dictionary with options:
Dcitionary<string, string> colors = new Dictionary<string, string>();
colors.Add("A", "#FFB97FC9");
colors.Add("B", "#FF9BCC50");
and then:
answerRectangle.Fill = GetColorFromHexa(colors[answer1]);

C# Array initialization with a ternary operator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
They don't seem to work in C#
string[] directions =
IsSomeValueTrue ? string[]{"RIGHT", "LEFT"} : string[]{"UP", "DOWN"};
string[] directions =
IsSomeValueTrue ? new string[]{"RIGHT", "LEFT"} : new string[]{"UP", "DOWN"};
What am i missing?
The shortest one-liner:
var dirs = IsSomeValueTrue ? new[] {"RIGHT", "LEFT"} : new[] {"UP", "DOWN"};
Try this:
string[] directions = IsSomeValueTrue ? new string[] {"RIGHT", "LEFT"} : new string[]{"UP", "DOWN"};

LINQ- Select linq from string array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How apply linq select to string array in C#
ex :
string[] result;
...
result.Select(..)
?
Thanks.
You pass in a lambda function that tells the system what you want to do with each string.
string[] result;
...
var newList = result.Select(s => {do something with s});
The function can do most anything that takes a string as an input and returns a value - it doesn't even have to return a string! For example, if the strings contained numeric characters, you could return a collection of numbers:
IEnumerable<int> newList = result.Select(s => int.Parse(s));
Note that the original array will not be changed.

Categories

Resources