论坛首页 编程语言技术论坛

c#学习笔记(五) 函数

浏览 2110 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-07   最后修改:2010-06-02

函数

格式:基本上合java类同

       修饰符 返回类型 方法名(参数列表){

           //do something

}

 

Main 函数

 程序的入口,有4种写法

static void main()

staric void main(String[] age)

static int main()

static int main(String age[])

 结构函数

 前面的文章提到,c#中有一种不同于java的数据类型叫做 结构,它还有一个重要的功能就是包含函数和数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{


    struct user
    {
        public int age ;
        public int age2 ;
        public int getSum(){
           return age+age2;
        }

    }
  
  
    class Program
    {
         
   
    
        /*
         * hello word
         * */
        static void Main(string[] args)
        {
            user u;
            u.age = 1;
            u.age2 = 2;
            Console.WriteLine(u.getSum());
            Console.ReadKey();
            
               
        
          

        }
      
     
    }
}

 函数的重载

这个没啥好说的,参照java

函数的委托(关键字: delegate)

委托是一种把引用存储为函数的类型,委托的声明类似于函数,但没有函数体。使用关键字:delegate

例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{


    delegate int te(int x, int y);
  
    class Program
    {

       
     static   int getm(int x,int y) {
            return x + y;

        }
        /*
         * hello word
         * */
        static void Main(string[] args)
        {
            te t;
            t = new te(getm);
          
            Console.WriteLine(t(1,2));
            Console.ReadKey();
            
               
        
          

        }
      
     
    }
}

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics