معنى كلمة indexers
السلام عليكم
يعطيكم العافيه جميعا لدي سؤال بسيط ما معنى كلمة indexers? وشكرا جزيلا |
السلام عليكم و رحمة الله و بركاته انا سعيد بتواجدى معكم هل من مرحب
|
واضحة اخي حسب ما اعرف (مصنفين)
|
كما هو معروف ،، فإن الـ Class يستخدم This كــــ keyword ،، و ذلك لتعريف الاندكسير Indexer ،،
و هذا من شأنه السماح للـ array بالوصول الى العناصر و المتغيرات بالـClass allow array-style indexed access to lists of elements in a class الـ Syntax للـ Indexer كالتالي : accessModifier returnType this[ IndexType1 name1, IndexType2 name2, … ] { get { // use name1, name2, ... here to get data } set { // use name1, name2, ... here to set data } } كن حذراً .. فإن تعريف الـ Indexer كـــ Static هو : syntax Error و هذا مثال للتوضيح ،، 1 // Box.cs 2 // Box class definition represents a box with length, 3 // width and height dimensions with indexers. 4 public class Box 5 { 6 private string[] names = { "length", "width", "height" }; 7 private double[] dimensions = new double[ 3 ]; 8 9 // constructor 10 public Box( double length, double width, double height ) 11 { 12 dimensions[ 0 ] = length; 13 dimensions[ 1 ] = width; 14 dimensions[ 2 ] = height; 15 } 16 17 // indexer to access dimensions by integer index number 18 public double this[ int index ] 19 { 20 get 21 { 22 // validate index to get 23 if ( ( index < 0 ) || ( index >= dimensions.Length ) ) 24 return -1; 25 else 26 return dimensions[ index ]; 27 } // end get 28 set 29 { 30 if ( index >= 0 && index < dimensions.Length ) 31 dimensions[ index ] = value; 32 } // end set 33 } // end numeric indexer 34 35 // indexer to access dimensions by their string names 36 public double this[ string name ] 37 { 38 get 39 { 40 // locate element to get 41 int i = 0; 42 while ( ( i < names.Length ) && 43 ( name.ToLower() != names[ i ] ) ) 44 i++; 45 46 return ( i == names.Length ) ? -1 : dimensions[ i ]; 47 } // end get 48 set 49 { 50 // locate element to set 51 int i = 0; 52 while ( ( i < names.Length ) && 53 ( name.ToLower() != names[ i ] ) ) 54 i++; 55 56 if ( i != names.Length ) 57 dimensions[ i ] = value; 58 } // end set 59 } // end string indexer 60 } // end class Box Box.cs 1 //BoxTest.cs 2 // Indexers provide access to a Box object's members. 3 using System; 4 5 public class BoxTest 6 { 7 public static void Main( string[] args ) 8 { 9 // create a box 10 Box box = new Box( 30, 30, 30 ); 11 12 // show dimensions with numeric indexers 13 Console.WriteLine( "Created a box with the dimensions:" ); 14 Console.WriteLine( "box[ 0 ] = {0}", box[ 0 ] ); 15 Console.WriteLine( "box[ 1 ] = {0}", box[ 1 ] ); 16 Console.WriteLine( "box[ 2 ] = {0}", box[ 2 ] ); 17 18 // set a dimension with the numeric indexer 19 Console.WriteLine( "\nSetting box[ 0 ] to 10...\n" ); 20 box[ 0 ] = 10; 21 22 // set a dimension with the string indexer 23 Console.WriteLine( "Setting box[ \"width\" ] to 20...\n" ); 24 box[ "width" ] = 20; 25 26 // show dimensions with string indexers 27 Console.WriteLine( "Now the box has the dimensions:" ); 28 Console.WriteLine( "box[ \"length\" ] = {0}", box[ "length" ] ); 29 Console.WriteLine( "box[ \"width\" ] = {0}", box[ "width" ] ); 30 Console.WriteLine( "box[ \"height\" ] = {0}", box[ "height" ] ); 31 } // end method Main 32 } // end class BoxTest BoxTest.cs أما نتيجة التنفيذ ،، فهي كالتالي : Created a box with the dimensions: box[ 0 ] = 30 box[ 1 ] = 30 box[ 2 ] = 30 Setting box[ 0 ] to 10... Setting box[ "width" ] to 20... Now the box has the dimensions: box[ "length" ] = 10 box[ "width" ] = 20 box[ "height" ] = 30 أتمنى أن يكون واضح ،، |
الساعة الآن 10:04 AM. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
جميع الحقوق محفوظة لـ : منتديات اليسير للمكتبات وتقنية المعلومات
المشاركات والردود تُعبر فقط عن رأي كتّابها
توثيق المعلومة ونسبتها إلى مصدرها أمر ضروري لحفظ حقوق الآخرين