This question already has answers here:
Reverse elements in an array
(6 answers)
Closed 6 years ago.
I need to create a function that receives an array items and reverses the order of items in the array and returns it.
Basically if I have an array ['cat', 'dog', 'bird', 'worm'] the function should return an array of ['worm','bird','dog','cat'].
So far I get lost in the function language... I have this.
//Split a string into an array of substring
string avengersNames = "Thor;Iron Man;Spider-Man;Hulk;Hawk Eye";
//Creat an array to hold substring
string[] heroArray = avengersNames.Split(';');
foreach (string hero in heroArray)
{
Console.WriteLine(hero);
}
//Parameter is Array of items
//At a loss when trying to incorporate an array into this function.
public static string[] heroList = new string[5];
{
}
Array.Reverse(heroArray); // can do trick for you
Use Array.Reverse function
string[] heroArray = avengersNames.Split(';');
Array.Reverse(heroArray );
Related
This question already has answers here:
Copy Arrays to Array
(7 answers)
Closed 1 year ago.
In C# and many other languages, if you pass an array to a function it passes the pointer/reference which means you can change the value of an array from inside a function.
From Microsoft:
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.
I have a special case where I need to access and change an array's contents from a function but I do not want to change the original array. I thought this would be quite simple. I could set a new array equal to the old array and change the new array. This acts the same, however, because the new array is just a pointer to the old one.
static void AddToArray(string[] array) {
var newArray = array;
newArray[2] = "y";
}
static void Main(string[] args) {
string[] array = new string[5];
array[0] = "h";
array[1] = "e";
AddToArray(array);
}
If you print the contents of array at each step:
"he"
"hey" (inside function)
"hey" (after function call)
I've done a lot of research online but somehow haven't found many other people who needed help with this. Advice is greatly appreciated!
You are not creating your array using "new" Keyword inside function. Change below line -
var newArray = array;
To
var newArray = new string[args.Length];
and after creating this as a new array, you can copy the values from args (passed) array
This question already has answers here:
Best way to combine two or more byte arrays in C#
(13 answers)
Closed 1 year ago.
I have the following list of arrays
var list = new List<string[]>();
how to use LINQ to convert/Join them into a single array
string[] singleArray;
It can be done by SelectMany operator.
var singleArray = list.SelectMany(x => x).ToArray()
This question already has answers here:
Convert string numbers to Array C#
(3 answers)
Closed 2 years ago.
for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.
This question already has answers here:
All possible array initialization syntaxes
(19 answers)
Closed 5 years ago.
I have this function in c#:
public string CommitDocument(string extension, byte[] fileBytes)
{
// some code here
}
I'm trying to call this function like this:
CommitDocument("document.docx", byte [1493]);
I'm getting an error: "Invalid expression term 'byte'". How can I pass a value to the parameter of type byte?
The byte[] is an array of bytes and you need to allocate the array first with 'new'.
byte[] myArray = new byte[1493];
CommitDocument("document.docx", myArray);
You defined CommitDocument as taking a byte array. A single byte is not convertible into a byte array. Or at least the Compiler is not doing that implicitly. There are a few ways around that limitation:
Provide a overload that takes a byte and makes it into a one element array:
public string CommitDocument(string extension, byte fileByte)
{
//Make a one element arry from the byte
var temp = new byte[1];
temp[0] = fileByte;
//Hand if of to the existing code.
CommitDocument(extension, temp);
}
Otherwise turn fileBytes in a Params Parameter. You can provide any amount of comma seperated bytes to a Params, and the compiler will automagically turn them into an array. See Params Documentation for details and limits:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params
This question already has answers here:
How to use Array.sort to sort an array of structs, by a specific element
(3 answers)
How would I sort through an array of structs?
(3 answers)
Closed 6 years ago.
Hello I would Like to receive some help. I have this structure:
struct data
{
public String names;
public int number;
}
I've been asked to show this structure in the console sorted alphabetically (by evaluating the names) I don't really know how to do this, I know how to sort arrays but i don't know how to sort a structure like this.
I am a beginner, any help is received thanks.
this might do the trick for you
data[] datas = new[] {
new data() { names = "Mohit", number = 3 },
//More data like that
}
and then
Array.Sort<data>(datas, (x,y) => x.names.CompareTo(y.names));
//or
Array.Sort(datas, (x,y) => string.Compare(x.names, y.names));
Or by using System.Linq
datas.OrderBy(x=>x.names);