C # tips

Source: Internet
Author: User

C # tips
C # introduction to some small knowledge points

Here we will explain some simple and small methods to improve the development speed.

Main method parameter Address Transfer

It is sometimes necessary to operate on two parameters, so it is complicated to implement it using the return value. To understand the reference transfer, for the value type: modifier ref modifier.

Parameter Quantity Method

When writing a method, sometimes it is worrying about the number of parameters. For example, some parameters want to use the value of the string if the value is called, and do not process it if the value is not passed. Here is a modifier params. If the number is not sure, use an array.

params int[]numbers

Circular comparison

To find the highest value in a set, use the cyclic comparison method, for example, to find the maximum value in an array. This programming idea can be applied a lot and requires divergent thinking.

  private static int GetMax(int[] arr)        {            int max = arr[0];            for (int i = 0; i< arr.Length; i++)            {               if (max < arr[i])               {                   max = arr[i];               }            }            return max;        }


Reserved decimal places

Some customers need to keep two decimal places, but some just happen to be one decimal place. The format method is used here. String numStr = string. Format ("{0. 00}", 2.3 );

String processing to find characters in a string

The return value of IndexOf (keyWord, index) is the index of the character, where the index starts to query, and keyWord is the character to be searched.

String cutting and splicing methods

  1、Split(new char[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries);


This is to split the string into arrays by spaces. Note that the following parameters remove the Null String.

2. string. Join ("", text); concatenate a string with spaces to connect elements in the string array

3. Convert the string into a character array ToCharArray ()

Compile the Indexer

Some objects can be called as they call arrays, for example, person [0]. In fact, this should have an index device"

    public string this[int index]        {            get { returnNames[index]; }            set { Names[index] = value;}        }


Reverse sorting

This example uses a simple array as an example.

 


Summary

This section does not include much new knowledge, but it is a small knowledge point. Some ideas are simple, but you need to understand that they can be used not only for arrays. It can also be used to compare a field of an object in a set.

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.