Fork me on GitHub

C#函数的学习 分类: C# 2012-05-14 00:51 567人阅读 评论(0) 收藏

函数就是将一堆代码进行重用的一种机制。函数就是一段代码,这段代码可能有输入的值(参数),可能会返回值。一个函数就像一个专门做这件事的人,我们调用它来做一些事情,它可能需要我们提供一些数据给它,它执行完成后可能会有一些执行结果给我们。要求的数据就叫参数,返回的执行结果就是返回值。

1)使用函数来连接字符串

函数的参数必须制定参数类型,和返回值的类型

static void Main(string[] args)
        {
            string str2 = SumString("abc", 3);
            Console.WriteLine(str2);
            Console.ReadKey();
        }
        static string SumString(string strBun,int nBtn )
        {
            string str="";
            for (int i = 0; i < nBtn; i++)
            {
                str = str + strBun;
            }
            return str;
        }

函数的所有路径都要有返回值,不能存在没有返回值的情况;

namespace 调用函数分割字符数组
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str2 = { "aa", "bb", "cc", "dd" };
            string strBre = "()";
            Console.WriteLine(BreakArray(str2, strBre));
            Console.ReadKey();
        }
        static string BreakArray(string [] str1,string strBreak)
        {
            string s = "";
            for (int i = 0; i < str1.Length - 1; i++)
            {
                s = s + str1[i]+strBreak;
            }
            s = s + str1[str1.Length - 1];
            return s;
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2012-05-14 00:51  zhiqiang21  阅读(246)  评论(0编辑  收藏  举报