zoukankan      html  css  js  c++  java
  • 个人学习一点小知识(我们不求多但求精)C# 方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 方法定义与调用
    {
    
        #region 方法的定义与调用
        class Program
        {
            public double MyMethod()
            {
                Console.WriteLine("this is MyMethod.");
                double i = 10.234;    //这个类型和上面 public double  MyMethod()要一致
                return i;
            }
            static void Main()
            {
                Program method = new Program();
                double j = 5;
                j = method.MyMethod(); //调用了double i = 10.234这句
                Console.WriteLine("the value is {0}.", j);
                Console.ReadKey();
    
            }
        }
    
        #endregion
    
    
        #region 方法中的参数传递(1.传递值类型的参数)
        class Program
        {
            public static void AddOne(int a)
            {
                a++;
    
            }
            static void Main()
            {
                int a = 33;
                Console.WriteLine("调用AddOne之前,a = {0}", a);
                AddOne(0);
                Console.WriteLine("调用Addone之后,a = {0}", a);
                Console.ReadKey();
            }
        }
        #endregion
    
    
        #region 方法中的参数传递(2.传递引用类型的参数(ref 参数类型 参数名))
        //ref 与传递值类型参数不同,引用类型的参数并没有再分配内存空间
        //实际上传递的是指向原变量的引用,及引用参数和原变量保存的是同一个地址。ref(Reference)
        class Program
        {
            public static void AddOne(ref int a)
            {
                a++;
    
            }
            static void Main()
            {
                int x = 3;
                Console.WriteLine("调用AddOne之前,x={0}",x);
                AddOne(ref x);
                Console.WriteLine("调用AddOne之后,x={0}",x);
                Console.ReadKey();
            }
        }
        #endregion
    
    
        #region 方法中的参数传递(3.输出多个引用类型的参数(out 参数类型 参数名))
        //out作用一个方法计算的结果有多个
        class Program
        {
            public static void MyMethod(out int a, out int b)
            {
                a = 5;
                b = 6;
            }
            static void Main()
            {
                int x, y;
                MyMethod(out x, out y);
                Console.WriteLine("调用Mymethod之后,x = {0},y = {1}",x,y);
                Console.ReadKey();
            }
        }
        #endregion
    
    
        #region  方法中的参数传递(4,传递个数不确定的参数)
    
        class Program
        {
            public static float Average(params long[ ] V)
            {
                long total, i;
                for (i = 0, total = 0; i < V.Length; ++i)
                    total += V[i];
                return (float)total / V.Length;
            }
            static void Main()
            {
                float x = Average(1, 2, 3, 5);
                Console.WriteLine("1,2,3,5的平均值为{0}", x);
                x = Average(4, 5, 6, 7, 8);
                Console.WriteLine("4,5,6,7,8的平均值为{0}", x);
                Console.ReadKey();
            }
        }
    
        #endregion
    
    
        #region 方法重载
        //方法重载是指具有相同的方法名,但参数类型后参数个数不完全相同的多个方法
        //可以同时出现在一个类中。这种技术非常有用,在项目开发过程中,我们会发现很多方法都需要使用重载技术
        class Program
        {
            public static int Add(int i, int j)
            {
                return i + j;
            }
            public static string Add(string s1, string s2)
            {
                return s1 + s2;
            }
            public static long Add(long x)
            {
                return x + 5;
    
            }
            static void Main()
            {
                Console.WriteLine(Add(1, 2));
                Console.WriteLine(Add("1", "2"));
                Console.WriteLine(Add(10));
                //按回车键结束
                Console.ReadLine();
    
            }
        }
        #endregion
    
    }
  • 相关阅读:
    PHP xml_parser_create_ns() 函数
    PHP xml_parse_into_struct() 函数
    PHP xml_parse() 函数
    PHP xml_get_error_code() 函数
    PHP xml_get_current_line_number() 函数
    文本竖排组合 | text-combine-upright (Writing Modes)
    文本溢出 | text-overflow (Basic User Interface)
    文本渲染 | text-rendering (Scalable Vector Graphics)
    文本最终对齐 | text-align-last (Text)
    文本属性-风格 | text-emphasis-style (Text Decoration)
  • 原文地址:https://www.cnblogs.com/ruishuang208/p/3099238.html
Copyright © 2011-2022 走看看