Home > Net >  Novice counseling, the generic form of the default type c #?
Novice counseling, the generic form of the default type c #?

Time:05-18

I have a method (below), for example, the return value I set as T shape, but found that there is a problem, every time use this method to add a & lt; Type name & gt; Parentheses, thank too bored lock, its sudden think so, to have this kind of grammar, if not add & lt;> Is a specified type by default, do not know to have a this syntax c #, please tall person to give directions, thank you very much!

 public static T OutErr (int returnCode=1, the string returnMsg="") 
{
JObject j=new JObject ();
JsonDo. OutReturnHead (j, returnCode returnMsg);
String a=typeof (T). The ToString ();
If (a=="System. The String")
Return (T) (object) j.t. oString ();
The else
Return (T) (object) j;
}


Can I think so, to T to specify a default type, such as:
 public static T OutErr (int returnCode=1, the string returnMsg="") 
{
JObject j=new JObject ();
JsonDo. OutReturnHead (j, returnCode returnMsg);
String a=typeof (T). The ToString ();
If (a=="System. The String")
Return (T) (object) j.t. oString ();
The else
Return (T) (object) j;
}

CodePudding user response:

Write a method package once

CodePudding user response:

There should be no direct support of grammar,

CodePudding user response:

Direct overloading just a matter of

 

Public static string OutErr (int returnCode=1, the string returnMsg="")
{
Return the default;
}

CodePudding user response:

Don't know JsonDo. OutReturnHead (j, returnCode returnMsg); This is what
Try this write
Public static object OutErr (int returnCode=1, the string returnMsg="")
{
JObject j=new JObject ();
JsonDo. OutReturnHead (j, returnCode returnMsg);
If (j.G etType ()==typeof (string))
Return j.t. oString ();
The else
Return j;
}

CodePudding user response:

The
reference 3 floor nan peony reply:
directly overloading is finished the

 

Public static string OutErr (int returnCode=1, the string returnMsg="")
{
Return the default;
}


Well, for now, has only the output of a string, find sometimes use behind the JObject type output, so we thought of unified methods, improve the utilization rate of the code!

CodePudding user response:

I wanted to think, don't need to,
Your internal logic also changed,
Overloading directly
  •  Tags:  
  • C#
  • Related