这样是可以计算出字符串的长度的。。
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)下面不知道怎么写

解决方案 »

  1.   

    如果你的方法和Main函数在一个类中
    直接 int n= yu("abcde");
    如果不在一个类中int n=Program.yu("abcde");
      

  2.   

    public static int yu(string str)
            {
                return  str == string.Empty ? 0 : Encoding.Default.GetByteCount(str);
            }yu("")
      

  3.   

    lz刚学吧,你写的类包括一个 静态的方法 static int 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();
            }
        }多看看书上例子就行了