这样是可以计算出字符串的长度的。。
static void Main(string[] args)
{ int n = 0;
string d = Console.ReadLine();
if (d == string.Empty)
{
n = 0;
}
else
{
n = Encoding.Default.GetByteCount(d);
}
Console.WriteLine(n);
Console.ReadLine();
Console.ReadLine();
但是我想用函数,如下:
class Program
{
public static int yu(string str)
{ int a = 0;
if (str == string.Empty)
{
a = 0;
} else
{
a = Encoding.Default.GetByteCount(str);
}
return a;
}在static void Main(string[] args)下面不知道怎么写
直接 int n= yu("abcde");
如果不在一个类中int n=Program.yu("abcde");
{
return str == string.Empty ? 0 : Encoding.Default.GetByteCount(str);
}yu("")
例如:using System;
class Program
{
public static void yu()
{
Console.WriteLine("调用yu方法");
}
static void Main(string[] args)
{
Program.yu();
}
}或者using System;
class Program
{
public static void yu()
{
Console.WriteLine("调用yu方法");
}
}
class text
{
static void Main(string[] args)
{
Program.yu();
}
}多看看书上例子就行了