I'm some what embarrassed to even ask this but I know there is a better way to do this I just don't know how
List<int> numbers = new List<int>(22);
numbers.Add(3);
numbers.Add(4);
numbers.Add(9);
numbers.Add(14);
numbers.Add(15);
//...
List<int> numbers = new List<int>(22) { 3, 4, 9, ..., 99 };
shorter than that? Only if your numbers follow a pattern which could be expressed mathematically.
This is the collection initializer.
You can use a collection initializer:
List<int> numbers = new List<int>(22)
{
3, 4, 9,
14, // ...
};
As of C# 3.0, at least, you can use an initializer, like so:
List<int> numbers = new List<int>{ 3, 4, 9, ... , 99 };
(Specifying the initial capacity (22) isn't terribly necessary...)
Related
I've been trying to write a program, in which I want to use the intersection of two HashSets. Therefore I wrote the following code (for test purposes):
HashSet<int> test1 = new HashSet<int>() { 1, 3, 5, 7, 9 };
HashSet<int> test2 = new HashSet<int>() { 1, 2, 3, 4, 5, 6};
HashSet<int> intersect = new HashSet<int>();
intersect = test1.Intersect(test2);
Line 5 shows an error (code CS0266) which - that's C#'s suggestion - can be corrected by change the line to:
intersect = (HashSet<int>)test1.Intersect(test2);
But when I run the program, the error appears again. I literally have no clue why, even after searching for an answer.
I want to achieve a intersection in the mathematical sense, so that the result for the variable intersect should be { 1, 3, 5}.
And what I found out - but couldn't test - is, that after using the intersect-method on test1, it changes the list in test1 to the intersection, is that true? If yes, is there any chance to avoid this? In my real program I don't want the variable to change into the intersection.
Should I just make a for-loop with an if-statement, to make my own intersection-method, or does this make the code worse?
As said, I tried to make use of C#'s suggestion, but this doesn't work either.
Because I'm a programming-beginner, I'm not really able to understand the definition of the intersect-method (because of this IEnumerable thing...), so I can't solve the problem using existend methods. And because I think my own method could be very inefficient, I don't to it my own. Furthermore I just want to understand, what's the problem. There are two HashSets, both containing integers, which should be intersected and saved in an extra variable...
Intersect() returns a IEnumerable<T>. You can use IntersectWith(), which modifies the current HashSet<T> object to contain only elements that are present in that object and in the specified collection:
HashSet<int> test1 = new HashSet<int>() { 1, 3, 5, 7, 9 };
HashSet<int> test2 = new HashSet<int>() { 1, 2, 3, 4, 5, 6};
test1.IntersectWith(test2); // we are altering test1 here
// test1 contains now [1, 3, 5]
or use the side-effect free Linq Intersect() to get an IEnumerable<T> and if you want it to be a new HashSet<T> just use a constructor:
HashSet<int> test1 = new HashSet<int>() { 1, 3, 5, 7, 9 };
HashSet<int> test2 = new HashSet<int>() { 1, 2, 3, 4, 5, 6};
HashSet<int> intersect = new HashSet<int>(test1.Intersect(test2));
// intersect contains now [1, 3, 5]
Remarks (from MSDN)
If the collection represented by the other parameter is a HashSet<T> collection with the same equality comparer as the current HashSet<T> object, this method is an O(n) operation. Otherwise, this method is an O(n + m) operation, where n is Count and m is the number of elements in other.
Basically in your case IntersectWith() is going to be more efficient!
Complete demo:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
HashSet<int> test1 = new HashSet<int>() {1, 3, 5, 7, 9};
HashSet<int> test2 = new HashSet<int>() {1, 2, 3, 4, 5, 6};
HashSet<int> intersect = new HashSet<int>(test1.Intersect(test2));
intersect.Dump();
test1.IntersectWith(test2);
test1.Dump();
}
}
Try it Online!
Enumerable.Intersect is a LINQ extension method that works with any kind of IEnumerable<T>. It returns IEnumerable<T>, so not a HashSet<T>. But since you have alrady two sets you want to use HashSet.IntersectWith(more efficient since O(n)) with manipulates the first HashSet<T>:
test1.IntersectWith(test2); // test1 contains now [1, 3, 5]
First. I'm sorry if the question the question is badly formulated because I don't really understand the question myself.
Hello,
I'm currently helping a friend with his practice tasks about 2-Dimensional Arrays.
The following task is: Expand the array with the missing columns. Fill the column with two values each.
Now my question is how? Is it even possible to put 2 values in one?
For a better understanding, see this code:
static int[][] BSP = {
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3 } ,
new int[] { 1, 2, 3, 4, 5, 6 }
};
Now we should expand the array to the task I mentioned above. I understand the Task like this:
Instead of one value there should be 2.
Can you help me or did I misunderstood the question.
When you are going to expand (or shrink) try to use list List<T> instead of array T[]. We want to extend lines while keeping columns intact, so we can use List<T[]> - list of arrays:
static List<int[]> BSP = new List<int[]>() {
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4, 5, 6 },
};
then you can just Add to extend (and RemoveAt to shrink):
// 4th row with 2 values: 12 and 21
BSP.Add(new int[] { 12, 21 });
I have 2 hash sets like this.
Hash_1 = {1, 2, 3, 4, 5}
Hash_2 = {4, 5, 6, 7, 8}
I am using C#
I want to compare those two sets and want to get the output like
Hash_3 = {1, 2, 3, 6, 7, 8}
What you want is: Hash_1 without Hash_2, and Hash_2 without Hash_1, then combined into one set.
So let's start with Hash_1 without Hash_2:
var modified1 = Hash_1.Except(Hash_2);
and then Hash_2 without Hash_1:
var modified2 = Hash_2.Except(Hash_1);
And now let's combine them:
var result = modified1.Concat(modified2);
Or in short:
var result = Hash_1.Except(Hash_2).Concat(Hash_2.Except(Hash_1));
Try it online
Or you could use SymmetricExceptWith
Modifies the current HashSet<T> object to contain only elements that
are present either in that object or in the specified collection, but
not both.
var h1 = new HashSet<int>() { 1, 2, 3, 4, 5 };
var h2 = new HashSet<int>() { 4, 5, 6, 7, 8 };
h1.SymmetricExceptWith(h2);
Console.WriteLine(string.Join(",", h1));
Output
1,2,3,7,6,8
Internally it just uses
foreach (T item in other)
{
if (!Remove(item))
{
AddIfNotPresent(item);
}
}
Source Code here
I can do this with an integer:
int a;
a = 5;
But I can't do this with an integer array:
int[] a;
a = { 1, 2, 3, 4, 5 };
Why not?
To clarify, I am not looking for the correct syntax (I can look that up); I know that this works:
int[] a = { 1, 2, 3, 4, 5 };
Which would be the equivalent of:
int a = 5;
What I am trying to understand is, why does the code fail for arrays? What is the reason behind the code failing to be recognised as valid?
The reason there is a difference is that the folks at Microsoft decided to lighten the syntax when declaring and initializing the array in the same statement, but did not add the required syntax to allow you to assign a new array to it later.
This is why this works:
int[] a = { 1, 2, 3, 4, 5 };
but this does not:
int[] a;
a = { 1, 2, 3, 4, 5 };
Could they have added the syntax to allow this? Sure, but they didn't. Most likely they felt that this use-case is so seldom used that it didn't warrant prioritizing over other features. All new features start with minus 100 points and this probably just didn't rank high enough on the priority list.
Note that { 1, 2, 3, 4, 5 } by itself has no meaning; it can only appear in two places:
As part of an array variable declaration:
int[] a = { 1, 2, 3, 4, 5 };
As part of an array creation expression:
new int[] { 1, 2, 3, 4, 5 }
The number 5, on the other hand, has a meaning everywhere it appears in C#, which is why this works:
int a;
a = 5;
So this is just special syntax the designers of C# decided to support, nothing more.
This syntax is documented in the C# specification, section 12.6 Array Initializers.
The reason your array example doesn't work is because of the difference between value and reference types. An int is a value type. It is a single location in memory whose value can be changed.
Your integer array is a reference type. It is not equivalent to a constant number of bytes in memory. Therefore, it is a pointer to the bytes where that data is stored.
In this first line, you are assigning null to a.
int[] a;
In the next line, if you want to change the value of the array, you need to assign it to a new array.
a = new[] {1, 2, 3, 4, 5};
That is why you need the new[] before the list of values within the array if you strongly type your declaration.
int[] a = {1, 2, 3, 4, 5}; // This will work.
var a = {1, 2, 3, 4, 5}; // This will not.
However, as many of the other answers have said, if you declare it in a single line, then you do not need the new[]. If you separate the declaration and initialization, then you are required to use new[].
{} syntax is available for array initialization, not to be used after declaration.
To initialize an array you should try like this:
int[] a = { 1, 2, 3, 4, 5 };
Other ways to Initializing a Single-dimensional array:
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] a = new int[5] { 1, 2, 3, 4, 5 };
Have a look at this: different ways to initialize different kinds of arrays
In C#, you can do the following:
List<int> registers = new List<int> { 1, 2, 3, 4 };
This will produce a list with 1, 2, 3, and 4 in the list. Suppose that I am given a list from some function and I want to insert a bunch of numbers like the following:
List<int> register = somewhere();
register.Add(1);
register.Add(2);
register.Add(3);
register.Add(4);
Is there a cleaner way of doing this like the snippet above?
Assuming the new items are in some kind of enumerable form already, the AddRange() method allows you to add them all in one go:
var toBeAdded = new int[] { 1,2,3,4 };
register.AddRange(toBeAdded);
You mean something like this?
List<int> register = somewhere();
register.AddRange(new List<int> { 1, 2, 3, 4 });
I think the cleanest you can get is this:
List<int> register = somewhere();
register.AddRange(new[] { 1, 2, 3, 4 });
However, it may not always be possible to implicitly type the array.