 |
|
[ ASP.NET ] การเรื่องค่าในตัวแปรไม่ให้ซ้ำกัน |
|
 |
|
|
 |
 |
|
เช็คตอนที่จะสร้างครับ 
ด้วยคำสั่ง Array.Exists
Code (C#)
static void Main(string[] args)
{
string[] planets = { "Mercury", "Venus",
"Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune" };
Console.WriteLine("One or more planets begin with 'M': {0}",
Array.Exists(planets, element => element.StartsWith("M")));
Console.WriteLine("One or more planets begin with 'T': {0}",
Array.Exists(planets, element => element.StartsWith("T")));
Console.WriteLine("Is Pluto one of the planets? {0}",
Array.Exists(planets, element => element == "Pluto"));
}
Code (VB.NET)
Public Sub Main()
Dim planets() As String = {"Mercury", "Venus",
"Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune"}
Console.WriteLine("One or more planets begin with 'M': {0}",
Array.Exists(planets, Function(element)
Return element.StartsWith("M")
End Function))
Console.WriteLine("One or more planets begin with 'T': {0}",
Array.Exists(planets, Function(element)
Return element.StartsWith("T")
End Function))
Console.WriteLine("Is Pluto one of the planets? {0}",
Array.Exists(planets, Function(element)
Return element.Equals("Pluto")
End Function))
End Sub
|
 |
 |
 |
 |
Date :
2017-12-25 13:47:13 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไปใช้พวก List<T> ดีกว่าครับ สามารถใช้ LINQ เขียนพวก Where ได้ด้วยครับ
|
 |
 |
 |
 |
Date :
2017-12-26 09:52:35 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
List(T)
|
 |
 |
 |
 |
Date :
2017-12-26 10:04:57 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
//ตัดตัวซ้ำของ Array ออก ใช้คุณสมบัติ Distinct
int[] s = { 1, 2, 3, 3, 4 };
int[] q = s.Distinct().ToArray();
// รวม Array 2D เป็น 1D
int[,] oGridCells = { { 1, 2 }, { 3, 4 } };
int[] oResult = new int[4];
System.Buffer.BlockCopy(oGridCells, 0, oResult, 0, 16);
|
 |
 |
 |
 |
Date :
2017-12-26 15:18:40 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|