C # Advanced Applications

Source: Internet
Author: User

Learning programming also has a big six months, think of the teacher said the day before yesterday: "You write code are pediatrics", suddenly have snack plug ... Think about it, it's impossible to write a for loop or a foreach loop to get a high salary? I also think that is not possible, I would like to give you a brief introduction of a more advanced application.

I. indexers : When you include arrays and collection members in a class, using indexers greatly simplifies access to arrays and collection members.

Here's the syntax:

1 [modifier]  datatype this    [index type index  ]2{3   Get {}// Get Property Code 4   set{}// Set Property Code         5  }

Modifiers include:public,protected,private,internal,new,virtual,sealed,override, Abstract,extern

Let me show you how to use:

1. First create a class, inside the class defines a length of 2 array, in the definition indexer, defined as follows.

      

  

2. In the main method, add a string that directly CW out the index of the string you want to print (you can also use loops to step through it).

    

3. The following is the result of the operation:

    

Two. Overloading of Operators : C # Allows user-defined types to overload operators by defining static member functions through the operator keyword.

1. Define the syntax for a "+" operator Overload:

    

1  Public Static int operator +(person P1,person P2)2{3   return p1+p2//  Object name attributes are added  ; 4 }

The other operators are roughly the same as those defined above.

Let me give you a brief introduction of his use:

1. First create a class, define two properties within the class, a name, an age property, as follows:

    

      

2. Called in the Main method, add the following:

    

3. Running Results

    

Because in the person class, the addition overload return is the sum of the age attributes of P1 and P2, two age is 20, and all the sum is 40.

C # Advanced Apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.