list c#
C#
List<int> list = new List<int>(); // List of ints
list.Add(5); // Add 5 to listusing System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify the type of part
// but the part name can change.
public class Part : IEquatable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
}
public class Example
{
public static void Main()
{
// Create a list of parts.
List<Part> parts = new List<Part>();
// Add parts to the list.
parts.Add(new Part() {PartName="crank arm", PartId=1234});
parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 });
// Write out the parts in the list. This will call the overridden ToString method
// in the Part class.
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// Check the list for part #1734. This calls the IEquatable.Equals method
// of the Part class, which checks the PartId for equality.
Console.WriteLine("\nContains(\"1734\"): {0}",
parts.Contains(new Part {PartId=1734, PartName="" }));
// Insert a new item at position 2.
Console.WriteLine("\nInsert(2, \"1834\")");
parts.Insert(2, new Part() { PartName = "brake lever", PartId = 1834 });
//Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
Console.WriteLine("\nParts[3]: {0}", parts[3]);
Console.WriteLine("\nRemove(\"1534\")");
// This will remove part 1534 even though the PartName is different,
// because the Equals method only checks PartId for equality.
parts.Remove(new Part(){PartId=1534, PartName="cogs"});
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
Console.WriteLine("\nRemoveAt(3)");
// This will remove the part at index 3.
parts.RemoveAt(3);
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
/*
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
Contains("1734"): False
Insert(2, "1834")
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
Parts[3]: ID: 1434 Name: regular seat
Remove("1534")
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1634 Name: shift lever
RemoveAt(3)
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1444 Name: banana seat
ID: 1634 Name: shift lever
*/
}
}
var intList = new List<int>();list[indexToEdit] = newItem;List<int> intList = new List<int>();var arr = new int[] {1, 2, 3};
List<int> list = arr.ToList();
Also in C#:
- Title
- roulette algorithm genetic algorithm
- Category
- C#
- Title
- how to convert a number to 2 decimal places in c#
- Category
- C#
- Title
- change textbox location C#
- Category
- C#
- Title
- unity how to end a game with esc
- Category
- C#
- Title
- how to raycast unit
- Category
- C#
- Title
- convert generic to type c#
- Category
- C#
- Title
- html hidden text
- Category
- C#
- Title
- unitt only 1 number float
- Category
- C#
- Title
- unity movetowards 2d
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- C# loop through array of objet
- Category
- C#
- Title
- count number of enum values C#
- Category
- C#
- Title
- how to pass an optional parameter in c# mvc asp.net
- Category
- C#
- Title
- nullreferenceexception c#
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- create line in unity
- Category
- C#
- Title
- unity check when clicked on object
- Category
- C#
- Title
- transform.Translate movement
- Category
- C#
- Title
- windows forms check if form is disposed
- Category
- C#
- Title
- loop through an enum c#
- Category
- C#
- Title
- c# get enum value from string
- Category
- C#
- Title
- get variable from another script unity
- Category
- C#
- Title
- convert string to int c#
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- div element position in screen
- Category
- C#
- Title
- c# print to console
- Category
- C#
- Title
- how to redirect to extern page in .net core
- Category
- C#
- Title
- replace double backslash with single backslash c#
- Category
- C#
- Title
- c# reverse a string
- Category
- C#
- Title
- Generate Genealogy view in mvc C# using Google Organizational Chart
- Category
- C#
- Title
- mongodb c# batch find
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- creating a c# class
- Category
- C#
- Title
- c# warning CS0108
- Category
- C#
- Title
- fahrenheit to celsius c#
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- unity c# get bool from another script
- Category
- C#
- Title
- unity c# set gameobject active
- Category
- C#
- Title
- unity mobile controls
- Category
- C#
- Title
- git checkout reset hard
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- 2d character controller unity
- Category
- C#
- Title
- c# dictionaries
- Category
- C#
- Title
- custom player spawner mirror
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- C# extend array
- Category
- C#
- Title
- .net core convert keycollection to array
- Category
- C#
- Title
- unity how to see what scen you are in
- Category
- C#
- Title
- assign datasource to dropdownlist in c#
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- panel drag all form c#
- Category
- C#
- Title
- remove from list based on condition c#
- Category
- C#
- Title
- unity to integer
- Category
- C#
- Title
- embed video to exe file with c#
- Category
- C#
- Title
- c# else if
- Category
- C#
- Title
- debug.log
- Category
- C#
- Title
- No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.'
- Category
- C#
- Title
- how to populate listbox using list<t> c#
- Category
- C#
- Title
- variables
- Category
- C#
- Title
- setting the parent of a transform which resides in a prefab
- Category
- C#
- Title
- microsoft.portable.csharp.targets was not found vs 2019
- Category
- C#
- Title
- idbset sqlquery
- Category
- C#
- Title
- set textbox colour to transparent c#
- Category
- C#
- Title
- foreach as parallel c#
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- .net core copy file in folder to root
- Category
- C#
- Title
- c# zip a file
- Category
- C#
- Title
- unity change text
- Category
- C#
- Title
- switch case c# range
- Category
- C#
- Title
- what type of variable is true or false in c#
- Category
- C#
- Title
- jump script unity 2d
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- how to get component in unity c#
- Category
- C#
- Title
- c# how to take from a float
- Category
- C#
- Title
- visual studio clear text script
- Category
- C#
- Title
- C# get object property name
- Category
- C#
- Title
- get type of variable c#
- Category
- C#
- Title
- pop up element from specific index in array
- Category
- C#
- Title
- unity deactive code from code
- Category
- C#
- Title
- wpf datatrigger enum binding
- Category
- C#
- Title
- c# check file exists
- Category
- C#
- Title
- check if network is available c#
- Category
- C#
- Title
- date time heutiges datum
- Category
- C#
- Title
- loop over enum values
- Category
- C#
- Title
- unity check tag
- Category
- C#
- Title
- 2d object look at object
- Category
- C#
- Title
- how to reference function in unity
- Category
- C#
- Title
- how to crouch in unity
- Category
- C#
- Title
- how to cast list to observablecollection c#
- Category
- C#
- Title
- JavaScriptSerializer() and convert to base64
- Category
- C#
- Title
- exception handling c#
- Category
- C#
- Title
- unity set material color
- Category
- C#
- Title
- Basic fps camera C#
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- how to hide a panel ui unity
- Category
- C#
- Title
- unity get scrollbar value
- Category
- C#
- Title
- No context type was found in the assembly
- Category
- C#
- Title
- c# list tuple
- Category
- C#