zoukankan      html  css  js  c++  java
  • [转载]C#控制台应用程序里调用自己写的函数的方法

     

    (2011-08-15 15:52:13)
    标签:

    转载

    分类: 技术

    最近写程序,遇到了一个很白痴的问题,记录下来,免得下次忘了。

    在C#控制台应用程序里调用自己写的函数的方法有两种:

    1. 将调用的函数设置成static

    2. 在Main里面实例化program,再调用。

    为什么不能在main里面用普通的函数调用方法呢?因为main是静态函数,他调用的本体函数也要求是static

    下面是举例:

    1 调用静态函数

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //取得自定义函数的返回值
    string msg=aa();
    //向控制台输出
    System.Console.WriteLine(msg);
    }
    //Main是static的,因此aa也要申明为static,否则无法访问
    private static string aa()
    {
    return "aa";
    }
    }
    }

    2 实例化

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

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Program p = new Program();
    string str = "";
    str=p.aa();
    Console.WriteLine(str);
    Console.ReadLine();
    }
    private string aa()
    {
    return "aa";
    }
    }
    }

  • 相关阅读:
    Intern Day7
    Intern Day7
    Intern Day7
    Intern Day6
    Intern Day6
    Intern Day6
    Intern Day6
    Intern Day6
    萧萧远树疏林外,一半秋山带夕阳
    飞线
  • 原文地址:https://www.cnblogs.com/alex-13/p/4737317.html
Copyright © 2011-2022 走看看