Home > Net >  Consult about c # constructor reference order.
Consult about c # constructor reference order.

Time:11-03

About the constructor, the following content is I already know:
 
Public partial class Form1: Form
{
Public _click ()//the default structure parameter
{
InitializeComponent();
}
Public _click (string filePath) : this ()//take the full path constructor
{
File. The Open (filePath, FileMode. Open);
}
Public _click (string filePath, string [] Para) : this ()//take synthetic route constructor
{
String FilePath=Path.Com bine (FilePath string. Join (", ", Para));
File. The Create (FilePath);
File. The Open (FilePath, FileMode. Open);
}
}



The above content is no problem, but not enough concise, to code reuse, I want to be is in the third constructor referenced in the second constructor, similar to the following code
 
Public _click (string filePath, string [] Para) : this ()//take synthetic route constructor
{
String FilePath=Path.Com bine (FilePath string. Join (", ", Para));
File. The Create (FilePath);
_click (FilePath);//this is clearly wrong, for realizing the function of the code
//this (FilePath), it is also wrong!
}



Excuse me, master how to implement, the File. The Open () represents a lot of code process

CodePudding user response:

Public _click (string filePath, string [] Para) : this (filePath)//need to synthetic path constructor
{
String FilePath=Path.Com bine (FilePath string. Join (", ", Para));
File. The Create (FilePath);
File. The Open (FilePath, FileMode. Open);
}

CodePudding user response:

Overloaded constructor can call each other

Public _click (string filePath) : this (string xx, xx) string

CodePudding user response:

 public partial class Form1: Form 
{
Public _click ()//the default structure parameter
{
InitializeComponent();
}
Public _click (string filePath) : this (filePath, null)//with the full path constructor
{
}
Public _click (string filePath, string [] Para) : this ()//take synthetic route constructor
{
If (Para!=null)
{
FilePath=Path.Com bine (filePath string. Join (", ", Para));
}
File. The Create (filePath);
File. The Open (filePath, FileMode. Open);
}
}

CodePudding user response:

The
reference exile on the second floor of the blade -- raven's reply:
overloaded constructor can call each other

Public _click (string filePath) : this (string xx, xx) string


I want to use _click (string filePath, string [] Para) create a constructor;
In Form1 (string filePath, string [] Para) internal call _click (string filePath)

CodePudding user response:

Public _click (string filePath, string [] Para) : this (filePath)//need to synthetic path constructor
{
String FilePath=Path.Com bine (FilePath string. Join (", ", Para));
File. The Create (FilePath);
}

CodePudding user response:

A concise, don't put the time and energy in the above,

CodePudding user response:

 
Public _click (string filePath, string [] Para) :
This (((Func) (()=& gt; {
String FilePath=Path.Com bine (FilePath string. Join (", ", Para));
File. The Create (FilePath);
Return FilePath.
()) {}}))
  •  Tags:  
  • C#
  • Related