在c#中我们如何将字符串传递给另一个函数

时间:2017-01-04 12:54:33

标签: c# .net

如何将字符串传递给c#中的函数 我在下面给出了示例代码

/* using stringbuilder method */
class Program
{
    public static void Main()
    {
        StringBuilder s = new StringBuilder("object");
        Console.WriteLine("orignal string ={0}", s);
        Console.WriteLine("string length  ={0}", s.Length);
        //appending string
        s.Append(" languge");
        Console.WriteLine("after appending={0}", s);
        //inserting string
        s.Insert(7," oriented ");
        Console.WriteLine("after inserting={0}",s);
        //setting a character
        int n = s.Length;
        s[n - 1] = '!';
        Console.WriteLine("final string   ={0}", s);
        length(s);
        Console.ReadLine();
    }
    public string length(string d)
    {
        int a = d.Length;
        return (a);   
    }
}

2 个答案:

答案 0 :(得分:2)

    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder s = new StringBuilder("object");
            Console.WriteLine("orignal string ={0}", s);
            Console.WriteLine("string length  ={0}", s.Length);
            //appending string
            s.Append(" languge");
            Console.WriteLine("after appending={0}", s);
            //inserting string
            s.Insert(7, " oriented ");
            Console.WriteLine("after inserting={0}", s);
            //setting a character
            int n = s.Length;
            s[n - 1] = '!';
            Console.WriteLine("final string   ={0}", s);
            length(s.ToString());
            Console.ReadLine();
        }
        static string length(string d)
        {
            int a = d.Length;
            return (a.ToString());
        }
    }

答案 1 :(得分:1)

函数长度也应该是静态的

您不能从静态函数(在本例中为main)中调用非静态函数。