با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن ، با دوره کامل آموزش #C در خدمت شما دوستان هستیم . در بخش قبلی با نوعی دیگر از مجموعه های غیر عمومی در زبان #C با نام Hashtable آشنا شدید. در این قسمت قصد داریم به معرفی و استفاده از Indexer در زبان #C و Indexer در ویژوال استودیو بپردازیم.
Indexer یک نوع خاصی از پراپرتی است که اجازه می دهد یک Class و یا struct به همان شیوه ای که یک آرایه به مجموعه های خودش دسترسی دارد، دسترسی پیدا کند.
ساختار کلی :
1 2 3 4 5 6 7 8 9 | Public <return type> this[<parameter type> index] { Get{ // return the value from the specified index } Set{ // set values at the specified index } } |
مثال زیر نشان می دهد که چگونه از indexer در کلاس سفارشی استفاده کنید :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | class StringDataStore { private string[] strArr = new string[10]; // internal data storage public string this[int index] { get { if (index < 0 && index >= strArr.Length) throw new IndexOutOfRangeException("Cannot store more than 10 objects"); return strArr[index]; } set { if (index < 0 && index >= strArr.Length) throw new IndexOutOfRangeException("Cannot store more than 10 objects"); strArr[index] = value; } } } class Program { static void Main(string[] args) { StringDataStore strStore = new StringDataStore(); strStore[0] = "One"; strStore[1] = "Two"; strStore[2] = "Three"; strStore[3] = "Four"; for(int i = 0; i < 10 ; i++) Console.WriteLine(strStore[i]); } } |
خروجی :
1 2 3 4 | One Two Three Four |
در مثال بالا، کلاس StringDataStore یک indexer برای آرایه رشته درونی خود را اجرا می کند. بنابراین در حال حاضر، آبجکت StringDataStore را می توان مانند یک آرایه برای اضافه کردن یا تکرار داده های رشته استفاده کرد. ما در مثال بالا آرایه رشته ای را استفاده کردیم، شما همچنین می توانید هر نوع مجموعه ای را که نیاز دارید استفاده کنید.
شما می توانید یک indexer را با انواع مختلف indexer ها ، باز نویسی کنید. مثال زیر نشان می دهد که چگونه indexer می تواند از نوع int و همچنین نوع رشته باشد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | class StringDataStore { private string[] strArr = new string[10]; // internal data storage public StringDataStore() { } public string this[int index] { get { if (index < 0 && index >= strArr.Length) throw new IndexOutOfRangeException("Cannot store more than 10 objects"); return strArr[index]; } set { if (index < 0 && index >= strArr.Length) throw new IndexOutOfRangeException("Cannot store more than 10 objects"); strArr[index] = value; } } public string this[string name] { get { foreach (string str in strArr){ if(str.ToLower() == name.ToLower()) return str.ToLower(); } return null; } } } class Program { static void Main(string[] args) { StringDataStore strStore = new StringDataStore(); strStore[0] = "One"; strStore[1] = "Two"; strStore[2] = "Three"; strStore[3] = "Four"; Console.WriteLine(strStore["one"]); Console.WriteLine(strStore["two"]); Console.WriteLine(strStore["Three"]); Console.WriteLine(strStore["FOUR"]); } } |
خروجی :
1 2 3 4 | One Two Three four |
ویژوال استادیو یک راه میانبر برای قرار دادن قطعه کد indexer فراهم کرده است به طوری که شما مجبور نیستید دستورات کامل را به صورت دستی بنویسید. برای قرار دادن قطعه کد indexer در ویژوال استودیو، indexer را نوشته و کلید Tap را فشار دهید و یا کلیک راست کرده (یا Ctrl + K، S) -> انتخاب “Insert Snippet.. -> انتخاب “Visual C#.. ” -> انتخاب “indexer”.
در این بخش شما با Indexer در زبان #C آشنا شدید ، در قسمت بعد قصد داریم در رابطه با ورودی و خروجی ها (Stream I/O) در زبان #C صحبت کنیم.