Ad Code

CS - 411 || Visual Programming Quiz 1 || CS 411 Solved by Vicky Lab || Normal Quiz August 2020


Solution File:

Auto-implemented properties improve the common-case we saw.  Here is the same example using auotimplemented properties.  Properties have the same idea as getters and setters.  They just allow simplified syntax.  Same idea of backing store and a simplified syntax for the general case is enabled by autoimplemented properties. 

  Using System; 

Public class Customer

{     Public int ID { get; set; }    

Public string Name { get; set; } } 

Public class autoimplementedcustomermanager

 {     Static void Main()    

{         Customer cust = new Customer(); 

        Cust.ID = 1;        

Cust.Name = "Amelio Rosales"; 

        Console.writeline(             "ID: {0}, Name: {1}",            

Cust.ID,             Cust.Name); 

        Console.readkey();     } }

An indexer enables your class to be treated like an array.  However you have your internal data representation not an actual array.  Its implemented using “this” keyword and square brackets syntax.  It looks like a property implementation. Here is an example.

Class intindexer

{     Private string[] mydata; 

    Public intindexer(int size)    

{         Mydata = new string[size]; 

        For (int i=0; i < size; i++)       

 {   Mydata[i] = "empty";         }

} 

    Public string this[int pos]   

 {         Get       

 {             Return mydata[pos];         }        

Set         {             Mydata[pos] = value;         }     } 

    Static void Main(string[] args)    

{         Int size = 10; 

        Intindexer myind = new intindexer(size); 

        Myind[9] = "Some Value";        

Myind[3] = "Another Value";       

 Myind[5] = "Any Value"; 

        Console.writeline("\nindexer Output\n"); 

        For (int i=0; i < size; i++)       

 {             Console.writeline("myind[{0}]: {1}", i, myind[i]);        

}     } }   The output of this program looks like this: 

Indexer Output  

Myind[0]: empty  Myind

[1]: empty  Myind

[2]: empty  Myind

[3]: Another Value myind

[4]: empty  Myind

[5]: Any Value  Myind

[6]: empty  Myind

[7]: empty  Myind

[8]: empty  Myind

[9]: Some Value 

Indexers can take any number of parameters. The parameters can be integers, strings, or enums. Additionally they can be overloaded with different type and number of parameters. The following example has overloaded indexers.  Using System; 

/// <summary>

///     Implements overloaded indexers.

 /// </summary> Class ovrindexer


Post a Comment

0 Comments

Close Menu