Is it possible to test an entire array at once? [duplicate] - c#

This question already has answers here:
Easiest way to compare arrays in C#
(19 answers)
Closed 3 years ago.
Is there a way to use an if statement to test an entire array at once by doing something like this:
if(myArray == {1,2,3})
{Debug.Log("This is quick")}
or do I need to iterate through each value in the array like this:
if(myArray[0] == 1 && myArray[1] == 2 && myArray[2] == 3)
{Debug.Log("This is not as quick")}

You should use SequenceEqual.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int [] myArray = {1,2,3};
int [] myArray2 = {1,2,4};
bool result = myArray.SequenceEqual(myArray2);
}
}

Related

Array returns System.Int32[]? [duplicate]

This question already has answers here:
System.Int32[] displaying instead of Array elements [duplicate]
(4 answers)
Closed 2 years ago.
When I make a function to return an array with the correct results.
Instead of giving me the correct results, I get as result System.Int32[].
Anyone an idea why this is?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MultiplyByLength(new int[] {2,3,1,0}));
}
public static int[] MultiplyByLength(int[] arr)
{
return arr.Select(x => x * arr.Length).ToArray();
}
}
You need to format it some how. An array doesn't have a ToString() override that knows how you want to format your type (int[]) to a string, in such cases it just returns the type name (which is what you are seeing)
foreach(var item in MultiplyByLength(new int[] {2,3,1,0})
Console.WriteLine(item);
or
Console.WriteLine(string.Join(Environment.NewLine, MultiplyByLength(new int[] {2,3,1,0}));
or
Console.WriteLine(string.Join(",", MultiplyByLength(new int[] {2,3,1,0}));

Why does this return 0? [duplicate]

This question already has answers here:
No overflow exception for int in C#?
(6 answers)
Closed 4 years ago.
using System;
namespace test_warmup
{
class Program
{
static void Main(string[] args)
{
int test = -1110835200;
test = test * 1700397056;
Console.WriteLine(test);
}
}
}
-1110835200 * 1700397056 = -1888860903781171200
Displayed in hex, that's -0x1a3694fc_00000000
In C#, int is only 32-bits, so the result is truncated to just 0.
In other words, the result of that multiplication is too large to fit in the variable to which you're assigning it, and the part that fits is all zero.

A Function with multiple outputs in C# [duplicate]

This question already has answers here:
Return multiple values to a method caller
(28 answers)
Closed 5 years ago.
I want to define a function with two outputs. The first one is a boolean variable and the second one is 2D array with unknown numbers of rows and columns but the array will be defined if the boolean variable is true and if the boolean variable is false, the array is not defined. how can I define this function? I am thankful if anybody can exemplify it in an example.
Thanks
You want something like this .
Tuple<string, int> NameAndId()
{
// This method returns multiple values.
return new Tuple<string, int>("Test", 100);
}
Why not return null if array is not defined?
public static bool MyMethod(out int[,] array) {
array = null;
...
}
....
int[,] data;
if (MyMethod(out data)) {
....
}
Or in case of C# 7.0+
if (MyMethod(out var data)) {
....
}
Edit: if you want to return an array, but you don't know its Length (or want to adjust it) you can try working with List<T> and put .ToArray() in the end:
using System.Linq;
...
List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(10);
...
list.Remove(5);
...
list.RemoveAt(0);
...
array = list.ToArray();

LINQ .Any() iterator for 2D Array - [,] [duplicate]

This question already has answers here:
Using Linq with 2D array, Select not found
(2 answers)
Closed 7 years ago.
What is the proper way to handle a 2D array with LINQ?
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
bool anyZeroes = array.Any(value => value == 0) // example
I want to check if any variable in the array matches a Func, == 0 in this case. How can I use Any for this and what is the best practice here?
Here's a way you can flatten the list to check
bool anyZeroes = array.Cast<int>().Any(value => value == 0);// false
bool anyNines = array.Cast<int>().Any(value => value == 9);// true
Though, if you are making multiple calls you should store it:
bool casted = array.Cast<int>();
bool anyZeroes = casted.Any(value => value == 0);// false
bool anyNines = casted.Any(value => value == 9);// true
Reference: https://stackoverflow.com/a/13822900/526704

Simple int array in unity 3d [duplicate]

This question already has answers here:
Adding values to a C# array
(26 answers)
Closed 9 years ago.
How can i create a int array in class.
And i have to add values to that array.
Not to a specific key.
i declared array as
public int[] iArray;
from function i have to insert values of i to array. My i values gets change. So i have to save those in a array.
iArray[] = i;
But it shows error.
Handling arrays is pretty straight forward, just declare them like this:
int[] values = new int[10];
values[i] = 123;
However, arrays in C# have fixed size. If you want to be able to have a resizeable collection, you should use a List<T> instead of an array.
var values = new List<int>();
values.Add(123);
Or as a class property:
class SomeClass
{
private List<int> values = new List<int>();
public List<int> Values { get { return this.values; } }
}
var someInstance = new SomeClass();
someInstance.Values.Add(123);

Categories

Resources