C # is partial, this keyword and the expansion method

One NO
partial: C # allows us to a class or interface structure divided into several sections, each implemented in several different .cs files.
Local type applies to the following conditions:
(1) type is particularly large, should not be placed in a file implementation.
(2) a portion of the code for the type of automation tools to generate code, should not be mixed with the code we have written.
(3) requires people to cooperate to write a class.
Local type restrictions:
(1) applies only to local type classes, interfaces, and is not supported commission and the enumeration.
(2) parts of the same type must have a modifier partial.
(3) When using the local type, a type of each part must be in the same namespace.
Each section (4) of a type to be compiled at the same time.
Code Example:
// partial1.cs:
namespace codesamples
{
public partial class MyPartialClass
{
public int the method1 (int X)
{
Console.WriteLine (X);
return 0;
}
}
}
// partial2.cs
namespace codesamples
{
public partial class MyPartialClass
{
public void method2(double x)
{
Console.WriteLine(x);
}
}
}
// program.cs
static void Main(string[] args)
{
MyPartialClass obj = new MyPartialClass();
obj.method1(11);
obj.method2(34.23);
Console.Read();
}

 

Two NO
C # extension methods
of some method of extending the concept: the
extension method is that you can add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type; extension method is a special static method, but the method can be called as instances on the same type of expansion, for C #, the calling and called extension method type no significant difference between the actual method defined; extension method, although the method is defined as static, but they by way of example is a method invocation syntax, their first argument specifies the type which acts on the method, and the parameter modifier of this prefix only when you are using directive explicitly namespace then introduced into the source code, the method was in the range of extension;

Code Example. 1:
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int the WordCount (String the this STR)
{
return str.split (new new char [] { '', '', ''},.?
StringSplitOptions.RemoveEmptyEntries) .Length ;
}
}
}
may be used a method using the extended instruction WordCount As for the range of:
using ExtensionMethods;
and the extension method can be called from the application using the following syntax:
String = S "the Hello the extension methods";
int I = s.WordCount () ;

Heart clarify matters point: In the code, a method may be used Examples of syntax to invoke the extension method, the compiler generates intermediate language will convert a static method call. So, not really violate the principle of encapsulation. Extension method can only be a member of the public to access the extension class

To facilitate understanding: looking at an example -> object extension method is a method of adding a conventional type, it can be either conventional type int, string data types, and may be self-defined data types. To add a method to understand the type of data: In general, there is a way to Tostring int data type is converted to int data type string, for example, now we want to add a little something into a string of time, such as adding a character a. so before Tostring not so that, because it's just that our int type data into a string, but it does not add a letter a. so which use the so-called method of expansion.

Binding at compile time extension method:
You can use extension methods to extend the class or interface, but can not override extension methods, interface or class method with the same name and signature of the extension method will never be invoked; compile time, extension methods always lower than the priority of an example method defined in the type itself. In other words, if a type has a method called Process (int i), and you have an extension method has the same signature, the compiler always bound to the instance method. When the compiler encounters a method call, it first looks for the matching method in this type of instance methods. If no matching method is found, the compiler will search for any type of extension methods defined and binds to the first extension method that it finds.

Extension method:

1. class where the method must be static
2. The method must be static,
the first parameter must be 3. The method you want to extend that type, such as int extension to give you a method, then the first argument on It must be int.
4. At a front of this parameter also requires a key
reference: HTTPS: //blog.csdn.net/zyh_1988/article/details/51103612
https://www.cnblogs.com/rinack/p/5695067. html

 

No Three

C # in this keyword
1.this represents the current instance of the object class
2. Using this series constructor
namespace Demo
{
public class the Test
{
public the Test ()
{
Console.WriteLine ( "no-argument constructor");
}
// this () constructor with no arguments corresponding to the Test ()
     // first execution Test (), after performing the Test (String text)
public the Test (String text): the this ()
{
Console.WriteLine (text);
Console.WriteLine ( "have parameters constructor ");
}
}

Program class
{
static void the Main (String [] args)
{
the try
{
the Test Test the Test new new = ( "John Doe");
}
the catch (Exception EX)
{
Console.WriteLine (EX);
}
the finally
{
Console.ReadLine ();
}
}
}
}
3. applicable to a certain type (type original or custom) extension method
4. achieved by indexing this, the program can be used to optimize the performance of

Guess you like

Origin www.cnblogs.com/wecc/p/11283939.html