In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the characteristics of visual studio 2010 naming and optional parameters in C#, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. Named parameters allow the caller to assign a value to the parameter by providing its name, so that the location of the parameter is no longer important. Optional parameters allow values to be assigned to certain parameters at definition time, and these "optional" parameters can be ignored when called. Named and optional parameters can be applied to methods, indexers, constructors, and delegates. Named and optional parameters are used in conjunction with the dynamic type to make it easy to access COM API such as Office Automation API.
1.1 named parameters
The syntax for named parameters is:
Parameter name 1: parameter value 1, parameter name 2: parameter value 2.
For example, the following code:
Static void Main (string [] args) {CreateUser (password: "adminpassword", name: "admin");
You can see that because named parameters are used in the call, the location of the parameters is no longer important.
1.2. Optional parameter
The definitions of methods, constructors, indexers, and delegates can specify whether their parameters are required or optional, and must be provided when called, but optional parameters can be omitted.
You can also define optional parameters using the System.Runtime.InteropServices.OptionalAttribute property class, which has been included in the base class library since the 1. 0 era.
The definition of each optional parameter contains a default value (the default must be constant), and if the parameter is not specified at the time of the call, the default value is used. For example, the following code:
Static void Main (string [] args)
{
CreateUser ("admin", "adminpassword", 50)
}
/ / /
/ / create a user
/ / /
/ / /
User name
/ / /
User password
/ / /
integration
/ / /
Whether or not to lock
Static void CreateUser (string name, string password
Int score=20,bool isLocked=false)
{
Console.WriteLine ("name: {0}, password: {1}", name, password)
}
An optional parameter is defined after all required parameters, and if the value of an optional parameter is provided at the time of the call, the values of all optional parameters before the optional parameter must be provided (if the parameter is preceded by an optional parameter). Parameters are not allowed to be provided in comma-separated form, that is, the following call is incorrect:
CreateUser ("admin", "adminpassword", true)
It must be written as follows:
CreateUser ("admin", "adminpassword", 20pm true);,
Or a better solution is to use named parameters
CreateUser ("admin", "adminpassword", isLocked:true)
1.3. COM API access
Naming and optional parameters, along with dynamic and other enhancements, make it easier to access COM API. For example, when calling some COM API in versions 3.0 or earlier, you need to use Type.Missing to omit certain parameters, such as the following code (code excerpt):
Var excelApp = new Microsoft.Office.Interop.Excel.Application ()
Var myFormat =
Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatAccounting1
ExcelApp.get_Range ("A1", "B4") .AutoFormat (myFormat, Type.Missing)
Type.Missing, Type.Missing)
But with the naming and optional parameters, you can simply write something like this:
ExcelApp.get_Range ("A1", "B4"). AutoFormat (Format: myFormat)
two。 Type equivalence support (Type Equivalence Support) (this paragraph is translated)
If you embed type information from a strongly named managed assembly, you can keep types in an application loosely connected to types in a separate release. This means that applications can use types in multiple versions of managed class libraries without recompiling each version.
Type embedding is often used for COM interactions, such as applications that use automation in Microsoft Office. Embedded type information allows the same application to run on machines with different versions of Office installed. And developers can use type embedding in a fully managed solution.
Types that can be embedded from a program need to meet the following conditions:
The assembly exposes at least one public interface.
The embedded interface is declared using the ComImport and Guid features
The assembly uses ImportedFromTypeLib and an assembly-level Guid feature annotation (Visual Basic and Visual C # templates already contain the Guid feature of the assembly by default).
After you specify public interfaces that can be embedded, you can create classes that implement those interfaces. The client program can reference the program that contains these common interfaces at design time and the default Embed Interop Types property is true to embed type information (the same effect can be achieved by using the / link compile switch on the command line), and the client can then create instances of these interfaces. If you create a new version of a strongly named runtime assembly, the client does not need to recompile with the new assembly; instead, the client program continues to use the version of the available assembly through the embedded type information of the common interface
2.1. First create a strong named interface class library (set properties according to conditions)
[ComImport] [Guid ("8DA56996-A151-4136-B474-32784559F6DF")] public interface ISampleInterface {void GetUserInput (); string UserInput {get;}}
2.2. Create a strong named class library, reference the interface class library, and define classes that implement the above interfaces:
Public classSampleClass: ISampleInterface
{
Private stringp_UserInput
Public stringUserInput {get {return paired UserInput;}}
Public voidGetUserInput ()
{
Console.WriteLine ("Please enter a value:")
P_UserInput = Console.ReadLine ()
}
}
2.3. Create a client application, reference the interface, and dynamically create the type using the reflective method to perform the appropriate action:
Class Program
{
Static void Main (string [] args)
{
Assembly sampleAssembly = Assembly.Load ("TypeEquivalenceRuntime")
ISampleInterface sampleClass =
(ISampleInterface) sampleAssembly.CreateInstance ("TypeEquivalenceRuntime.SampleClass")
SampleClass.GetUserInput ()
Console.WriteLine (sampleClass.UserInput)
Console.WriteLine (sampleAssembly.GetName () .Version.ToString ())
Console.ReadLine ()
}
}
4. Modify the client class that implements the interface, add a new method, and modify the assembly version number and file version number to 2.0.0.0:
Public DateTime GetDate () {return DateTime.Now;}
5. Execute the client program again and observe the difference (the client will output the new version number).
Assemblies created entirely in managed code in .NET automatically recognize updates, that is, without the need to use additional property definitions, directly create interfaces, implement interface class libraries and client classes (or no interfaces directly create class libraries on the client side reference), copy to the client reference location after the class library update, the client will automatically detect the update, which is also the benefit of the .NET assembly for developers. But where is the use of type equivalence support reflected, I think it is convenient for COM API access, because COM may be written in other languages, there is no way to automatically sense version changes like .NET assemblies, personal opinions, expect expert answers.
Thank you for reading this article carefully. I hope the article "what are the characteristics of naming and optional parameters of visual studio 2010 in C#" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.