[C #] foundation method returns a value, enumeration, partial classes, extension methods

Methods return multiple values, not the same type of time how to deal with?

1: declare a class or structure to return information are defined as members of the type.

2:out

3: Type tuple Tuple

enumerate:

Enum.GetNames (typeof (Color)) returns a string array that contains the names of all enumeration

Enum.GetValues ​​(typeof (color)): Gets an enumeration of all values, but this side of the item declared as an array type,

Otherwise, the enumeration does not return a value corresponding to, or will return and GetNames the same results.

 public enum Color
    {
        Red=1,
        Blue=2,
        Yellow=3
    }
1   foreach (var item in Enum.GetNames(typeof(Color)))
2   {
3      Console.WriteLine(item);
4   }
5 
6foreach (int item in Enum.GetValues(typeof(Color)))
7  {
8    Console.WriteLine(item);
9  }

In turn returns the result:

 Section of the class:

allows partial keyword class, structures, methods, interfaces in a plurality of files.
The return value must be part of the method Void type.

 Extension method:

Extension class way:

1: Inheritance

2: Extension Methods

Description: When the time can not use inheritance, this option may be used (e.g. a seal when the class)

Extension method may also be used to extend the interface. All such class that implements this interface will have a public function

If you implement extension methods:

1: class extension methods and extension methods must be on static static logo.

2: Use this type of keywords and mentioning the need to extend a parameter.

3: non-extension method must be defined in a generic static class.

Note: This tag this extension method parameter of type string can call.

It may be behind the increase in the required parameters. You actually need to be based, the latter does not need to use this parameter to mark up.

如: .....(this string str,int  age,......)

1   public static class StringExtension
2     {
3         public static int GetWordCount(this string str) => 
4         str.Split().Length;
5     }

 

example:

Guess you like

Origin www.cnblogs.com/SignX/p/10990636.html