c# adding a string to an array [duplicate] - c#

This question already has answers here:
How to add a string to a string[] array? There's no .Add function
(15 answers)
C# declare empty string array
(10 answers)
Closed 7 years ago.
i want to simply add a string to an array, like this:
string[] arrayName = new string[0];
arrayName.Add("raptor");
But this doesn't work, can someone help me?

You should use a generic List(Of T).
List<string> myStrings = new List<string>();
myStrings.Add("raptor");
and if you really want an array:
string[] myStringArray = myStrings.ToArray();

Related

How to check if element already exist in an array [duplicate]

This question already has answers here:
Method to check array list containing specific string
(4 answers)
Closed 3 years ago.
I got an ArrayList in C#
ArrayList myAL = new ArrayList();
now I add a string to it
myAL.Add("Hello");
How can I now figure out if the String "Hello" exist in the array. I know it's possible to loop over it, but I think there must exist a function or something.
You can simply use the method Contains from System.Collections
Microsoft Doc - Contains
For your example :
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
if (myAL.Contains("Hello"))
{
// Do something
}

How to place an Array like a row in a List<string> [duplicate]

This question already has answers here:
Conversion of System.Array to List
(11 answers)
Closed 6 years ago.
How do I place an Array like a row in a List?
For Example:
string[] Myarray = new string[] {"A","B", "C"};
List<string> MyList = new List<string>() { Myarray[0], Myarray[1], Myarray[2] };
List<String> strings = Arrays.asList(new String[]{"one", "two", "three"});
This is a list view of the array, the list is partly unmodifiable, you can't add or delete elements. But the time complexity is O(1).
If you want a modifiable a List:
List<String> strings =
new ArrayList<String>(Arrays.asList(new String[]{"one", "two", "three"}));
This will copy all elements from the source array into a new list (complexity: O(n))

how to instanciate a list with an array of list like in the example? [duplicate]

This question already has answers here:
Initializing collections that contain nested collections
(3 answers)
Closed 6 years ago.
I have to make two lists in C # . How do the second list ?
The first is well and works , the second already tried as exemplified below but does not work . And I want it to be exactly like or similar without the use of other variables
List<String> t = new List<string>() {"question","anotherQuestion"};
then i want another list
and i would like to initialize it like this.
List<String[]> t = new List<string[]>()
{
("rightawnser","wrongAwnser"), //1st question
("rightawnser","wrongAwnser"), //2nd question
};
Close, but you have to create an array for each list entry:
List<string[]> t = new List<string[]>()
{
new[] { "rightawnser","wrongAwnser" }, //1st question
new[] { "rightawnser","wrongAwnser" }, //2nd question
};

How to get part of a string[] array in C# [duplicate]

This question already has answers here:
How do I clone a range of array elements to a new array?
(26 answers)
Closed 7 years ago.
How, using C#, can I create a new string[] from another string[] containing a subsection of the old string[] array?
For example, if I had:
string[] a = { "cat", "dog", "hamster", "parrot" }
And I wanted to get everything after the first element I should get:
string[] b = { "dog", "hamster", "parrot" }
How, if this is even possible without a for loop, do I achieve this?
You can use some extensions methods to solve it, that is the case for Skip(int) under the System.Linq namespace, for sample:
string[] b = a.Skip(1).ToArray();
And it will skip 1 element and convert the result into a new array. Remember to add the namespace:
using System.Linq;
As the MSDN documentation said:
Bypasses a specified number of elements in a sequence and then returns
the remaining elements.

Object refrence not set to an instance of an object in array [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
i am very new to C# programming so my question may sound very silly
actualy i am creating an multidimensional string array like
public class master
{
public List<string> user_selected = new List<string>();
public List<string> available = new List<string>();
public List<string> bookedseats = new List<string>();
public string [] [] trackbooked=new string[30][] ;
}
now i am assigning some values to it like
a[l].trackbooked[i][j] = pb.Name;
a is a list of object
List<master> a = new List<master>();
a.Add(obj0);
a.Add(obj1);
a.Add(obj2);
a.Add(obj3);
a.Add(obj4);
can some one plz help.thank u in adv.
You've only initialized one dimension of your multidimensional array. See msdn for all the ways you can initialize a multidimensional array.
public string [,] trackbooked=new string[30,30] ;
pb.Name.Tostring();
I would venture to guess that whatever pb is that its NAME value isn't returned as a string. You should be able to use the above command to correct your issue.

Categories

Resources