Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is there a difference between the following In terms of performance?
private static readonly string[][] a = { new string[] {"a", "b", "c"}};
private static readonly string[][] a = { new string[3] {"a", "b", "c"}};
Assuming I can write the 2 options (I know the values that will be in the array beforehand) which is better / more correct?
Both generates the same IL codes, so same performance, and both are correct.
The only advantage of the second option is when you need to restric the number of elements, it will give you a compile error if you put more or less elements.
// error
private static readonly string[][] a = { new string[3] {"a", "b"}};
Related
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]));
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Why can't I just re-declare an array with commas?
static void Main(string[] args)
{
short[] arr = new short[6] { 1,1,1,1,1,1 };
if(1)
{
arr = {1,0,0,1,1,0}; // this line doesn't work
}
}
The initialization expression is not {1,0,0,1,1,0}
The initialization expression must be new short[6] { 1,1,1,1,1,1 }
So, essentially, the statement of your question is the answer to your question.
This syntax : short[] arr = {1, 0, 0, 1, 1, 0};
is called array initialization syntax and it works only in declaration.
why ?
As the guy here wrote, it just how Microsoft guys choose to implement for some reason.
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);
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
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.
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"};