Devdit
 

error CS1061: 'string' does not contain a definition for 'Count' คือ วิธีแก้ไข

1.1K

ฝึกเขียนภาษา C# ต้องการใช้คำสั่ง Count เพื่อนับจำนวนข้อมูลใน Array แต่พอรันแล้วขึ้น error CS1061: 'string' does not contain a definition for 'Count' แบบนี้ต้องแก้ไขอย่างไร

using System;

class Program
{
    static void Main(string[] args)
    {
        int[] n = {5, 6, 10, 8, 7};
        int c = n.Count();
        Console.Write( "ตัวแปร array มีข้อมูลทั้งหมด "+c+" ข้อมูล" );
    }
}

 

วิธีแก้ไข

ภาษา C# คำสั่ง Count ต้อง using System.Linq; เข้าในโปรแกรมก่อนจึงจะสามารถเรียกใช้คำสั่ง Count ได้ แนะนำให้เพิ่มคำสั่ง using System.Linq; ไว้ที่ด้านบนใต้บรรทัด using System;

using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int[] n = {5, 6, 10, 8, 7};
        int c = n.Count();
        Console.Write( "ตัวแปร array มีข้อมูลทั้งหมด "+c+" ข้อมูล" );
    }
}

ผลลัพธ์

ตัวแปร array มีข้อมูลทั้งหมด 5 ข้อมูล
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ