How to pass array to method [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 7 years ago.
Improve this question
It's probably a stupid question, but anyway.
My problem is that I can't pass uninitialized array, but I don't know if my array need to hold 5 or 30000 elements, eg. so it will be a waist of memory to initialize big array.
Should I use List<T> instead, or?
I've noticed people tend to return array instead of list, which is mutable, and therefore much more convenient, so there must be a performance issue with lists. Is that so?

make it into an 'out' parameter and all should be well:
private void x()
{
string sTestFile = "this is a test";
string[] TestFileWords;
FixConcatString(sTestFile, out TestFileWords);
}
private void FixConcatString(string splayfile, **out** string[] sWordArray)
{
char[] charSeparators = new char[] { '-' };
splayfile = splayfile.ToLower();
splayfile = splayfile.Replace(#"\", " ");
sWordArray = splayfile.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
}

Related

How to number values in string array with int [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 3 years ago.
Improve this question
I have some strings in string array.
Now I wanna to give before first string prefix 1: string..etc
Example:
Test
Test
Test
Expected result:
1. Test
2. Test
3. Test
Is that even possible? Thanks!
You could use Enumerable.Select (Linq) with Index, and use string formatting to include Index as prefix of string.
For Example
var result = strCollection.Select((x,index)=> $"{index+1}.{x}");
If you want to do this in-place with an array of strings (without creating a new array), you can't use Linq. Instead, just use something like this:
public static void AddNumberingPrefix(string[] strings)
{
for (int i = 0; i < strings.Length; i++)
{
strings[i] = $"{i+1}. {strings[i]}";
}
}
Example usage:
string[] items = {"First", "Second", "Third"};
AddNumberingPrefix(items);
Console.WriteLine(string.Join("\n", items));
This outputs:
First
Second
Third
There is an overload of LINQ Select that gives you the index:
yourList.Select((el, idx) => $"{idx+1}. {el}");

Parameters in parentheses after a method name: what are they and what do they do? [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 3 years ago.
Improve this question
Newb to C# and OOP. My journey thus far has been to take code bases that I've inherited from former developers and either address issues, or add enhancements, whilst trying to understand said code bases' structures from front-to-back.
I'm having trouble fully grasping the concept around the parameters which follow the initial declaration of a method. Here's an example of a method I'm working with:
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity entityManagement = new Entity();
string[] lineParts = line.Split('|');
entityManagement.Identifier = lineParts[0];
entityManagement.ProductId = 1234;
entityManagement.Category = "ABCDE";
entities.Add(entityManagement);
}
return entities;
}
The part after ParseCsvFile in parentheses: (List<string> entries, string urlFile)
Could someone explain what these are and what they do, perhaps with metaphors/analogies/real-world examples?
It might be easier to see their purpose if you look at a simpler function for example:
public int Add(int number1, int number2)
{
return number1 + number 2;
}
Above there is a function that adds two numbers together and returns the result. It is a set of instructions to follow. How can it follow the instructions if it doesn't know what numbers to use.
That's where calling the function comes in.
for example:
var result = Add(2, 5);
In this scenario result = 7.
2 is replacing number1 in the function and 5 is replacing number2.

How can I make each array, within an array perform a task? [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 6 years ago.
Improve this question
I am getting data with one of the following names, Adam, Bob, Cam, Dan, Earl, or Fred.
I only want certain pairs to operate on each other. Right now I have the string:
string list="Adam-Bob;Cam-Dan;Earl-Fred";
Then I split them via the semicolon
string[] splitList=list.Split(';');
Now I have an array of pairs as so
Adam-Bob Cam-Dan Earl-Fred
[0] [1] [2]
Ideally, I would like to perform an operation on each of them, but instead I find that I can only do the following:
Split via ','
foreach (string s in splitList)
{
string firstPerson=splitList[0];
string secondPerson=splitLilst[1];
if (UDPoutputData.Contains(firstPerson)==true)
{
//record data into string for firstPerson
}
if (UDPoutputData.Contains(seoncdPerson)==true)
{
//record data into string for secondPerson
}
//if I have data for firstPerson AND secondPerson, perform operation and give me the output
}
Unfortunately, if I get the name Adam, followed by Cam, my operations are disorganized. Perhaps I need to automatically create a string for each name? Or is there an eloquent way of operating the data on the first array...
You could get an array of arrays (of string), like this:
string[][] splitList = list.Split(';').Select(pair => pair.Split('-')).ToArray();
Then you can access splitList[0][0] to get Adam, splitList[0][1] would be Bob, splitList[1][0] would be Cam, etc.
So your loop becomes:
foreach (string[] pair in splitList)
{
string firstPerson=pair[0];
string secondPerson=pair[1];
// ...

Sort student array according to average marks [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 6 years ago.
Improve this question
I have created student class array in C# like this
var Student = new Student[5];
having variables sid,name, avgMrks;
I want to sort array according to average marks of all the students.
I assuming when you say having variables, you mean that the Student object has the properties: sid, name, avgMrks. You can do:
Student.OrderBy (x=>x.avgMrks);
Use LINQ
Student = Student.OrderByDescending(c => c.avgMrks).ToArray();
It returns IOrderedIEnumerable, which you can convert back to Array if you want.
Or
string[] ArrStr = new string[] { "A", "A2", "A1" };
Array.Sort(ArrStr);
Array.Reverse(ArrStr);

Getting individual string length in an array [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 8 years ago.
Improve this question
I am trying to write a program which does the following:
There is a method called lengthOffTheWords. It receives an array of strings, and returns an array of numbers which represent the length of each individual string.
Ex: For the following input
{"I", "know", "a" , "friend"} the method returns {1,4,1,6} .
{"yes"} the method returns {3}.
{"me", "too"} the method returns {2,3}.
I would like to see an example of how to write it.
I would do something like this:
public int[] LengthOffTheWords(string[] array)
{
return array.Select(item => item.Length).ToArray();
}
I did not test this but it should do what you want.
int[] GetLengths(string[] array)
{
int[] structure = new int[array.Length];
for(int i = 0;i < array.Length;i++)
{
int length = array[i].Length;
structure[i] = length;
}
return structure;
}

Categories