博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
结构体,数组,方法的重载,ref,out
阅读量:7255 次
发布时间:2019-06-29

本文共 2121 字,大约阅读时间需要 7 分钟。

 

1. 结构体,数组。

 

 

namespace Struct_array结构体数组{    //结构体的声明,关键字是Struct。    struct people    {       public char sex; //结构体里面不但能存放属性,还能存放方法。       public int age;  //如果属性不用public修饰,外面就不能访问该属性。       public void sayHello() { Console.WriteLine("hello,Struct!"); }    }    class Program    {        static void Main(string[] args)        {            people Jim;  //使用结构必须先声明变量。            Jim.age = 11;            Jim.sex='男';            Jim.sayHello();  //结构体里面的方法调用            Console.WriteLine("Jim 今年"+Jim.age+"岁了。");            Program ss = new Program();            ss.arrayexpra();            Console.ReadKey();        }              public void arrayexpra()        {            int[] nums;                            nums = new int[] { 1, 2, 3 };              int[] names=new int[10]; //声明一个最大长度为十的数组;            int[] ages = { 3,2,1,4,5,8,7}; //直接声明一个数组。             Array.Sort(ages);  //对数组排序                       for (int i = 0; i < ages.Length; i++)            {                Console.WriteLine(ages[i]);            }            Array.Reverse(ages); //反转一个数组            for (int i = 0; i < ages.Length; i++)            {                Console.WriteLine(ages[i]);            }            }    }}

 

2.函数。

 

namespace 方法的重载{    class Program    {        static void Main(string[] args)        {            Program p = new Program();            Console.WriteLine(p.fun("1",2));            int i,j=100;            p.funOut(2, 4, out i);  //out参数方法的调用,i可以不付初始值。            Console.WriteLine(i);            p.funref(2, 4, ref j); //ref参数方法的调用,j不可以不付初始值。            Console.WriteLine(j);            Console.ReadKey();        }        int fun(string i, int c) //方法的重载,就是一样名字的两个不同方法。这样做为了方便程序远记忆繁杂的函数名称。        {            return Convert.ToInt32(i) + c;        }        string fun(int r, int c, int b)        {                       return (r + c + b).ToString();        }        void funOut(int r, int c, out int b)//out参数方法,b必须赋值。        {            b = 100;                              b = r + c + b;        }        void funref(int r, int c, ref int b)  //ref参数方法        {                       b = r + c + b;        }    }}

 

 

 

转载地址:http://skkdm.baihongyu.com/

你可能感兴趣的文章
(补充)9.Struts2中的OGNL表达式
查看>>
1572: [Usaco2009 Open]工作安排Job
查看>>
Django Rest Framework之版本控制
查看>>
网关协议学习:CGI、FastCGI、WSGI
查看>>
梯度下降算法以及其Python实现
查看>>
[bzoj 1355][Baltic2009]Radio Transmission
查看>>
Antialiasing with Transparency
查看>>
c# 类一般在哪里实例化,是在类内、方法内还是其他地方?
查看>>
自定义 checkbox、 radio 样式
查看>>
杭电2141--Can you find it?
查看>>
BOM DOM 简介
查看>>
[转载]Oracle修改表空间大小
查看>>
Oracle函数学习总结(1)
查看>>
javascript函数和内置对象
查看>>
Hadoop本地库
查看>>
Python中修改文件的核心思路
查看>>
解决 AX 2009 客户端运行时"An invalid directory structure for Microsoft Dynamics AX was detected"错误...
查看>>
dubbo结构及通信简介
查看>>
跳出iframe
查看>>
Farthest Nodes in a Tree (求树的直径)
查看>>