C#知识点总结

C#知识点总结

using System; //System是一个命名空间。

一 输入输出

从屏幕上获取输入

   i=int.Parse(Console.ReadLine());

输出到屏幕上:

   System.Console.WriteLine(arr[0]);或

Console.WriteLine("arr[{0}]={1}",i,arr[i]);和C语言中的输出类似

二 数组

建立一个数组

   int [] arr=new int[3];

   可直接赋值 int [] arr=new int[] {1,2,3};

   也可以单独赋值:

   arr[0]=0;

   arr[1]=1;

   arr[2]=2;

数组的输出

for(int i=0 ;i<arr.length ;i++)

{

   Console.WriteLine(arr[i]);

}

或用

foreach(int i  in  arr)

{

       Console.WriteLine(“arr[{0}]={1}”, i  , arr[i]);

}

动态数组的实现

遗憾的是:C#不支持动态数组,那么我们怎么来实现动态数组呢?用函数!

我这里直接粘过来今天所写的代码

这里用了两个类SetArray和test,Main函数在test里,若把实现动态数组的函数直接放在test中,需要用static修饰函数名而不是Public。因为Main函数是Static。

using System;

class SetArray

{

/*

基本思想:ArrLength 是你想要动态数组的大小,函数的作用也就是一个传数组大小的工具,其他的和建普通数组一模一样。

*/

      public void PrintArr(int ArrLength)  //动态数组的实现,

      {

             int [] arr=new int[ArrLength];

             for(int i=0;i<arr.Length;i++)

                    arr[i]=i;

             Console.WriteLine("Print Array's value");  //输出一行字

             foreach(int i in arr)                    //输出数组里的数据

                    Console.WriteLine("arr[{0}]={1}",i,arr[i]);

      }

}

class  test

{

     

      static void Main()

      {

             SetArray arr=new SetArray();        

//实例化Class SetArray。方便调用里面的函数。

             int i=1;

             while(i>0)

             {

                    Console.WriteLine("Please enter the array's Length:");

                    i=int.Parse(Console.ReadLine());    //输入你想要的数组大小

                    arr.PrintArr(i);                   //调用函数

             }

      }

}

三 字符数组

字符数组的建立

ArrayList arr=new ArrayList();

 

字符数组的输入和输出

字符数组不需要事先决定数组大小,可以算是动态的字符串数组,如果数字动态数组都是个位数的话可以通过字符数组来实现。

使用字符数组要调用 using System.Collections;

using System;
using System.Collections;
class Arrlist
{
    static void Main()
    {
        ArrayList arr=new ArrayList();   //使用字符数组的类库
        String str1;                    //声明字符串变量
        while(true)
        {
            Console.WriteLine("Please add a string to ArrayList: ");
            str1=Console.ReadLine();
            if(str1=="end")
                break;
            arr.Add(str1);                 // 字符串数组增加一个值
            Console.WriteLine();
            for(int i=0 ;i< arr.Count;i++)     //arr.Count 的值为字符串数组现有的字符串数量
                Console.Write("{0} ",arr[i]);   
            Console.WriteLine("\n");
        }
    }
}

运行结果:

 

注意:C#中Console.Write() 与 Console.WriteLine() 的区别和

      Java中System.out.Print() 与System.out.println()的区别一样,后面的是自带换行,前面的不换行

 

四 二维数组与多维数组

二维数组的建立

int[ , ] arr=new int[4,6]; 

中间加一个逗号就是二维数组,加两个逗号就是三维数组,以此类推。

数组的赋值和C/C++中多维数组的赋值相同。

int[,] arr=new int[2,3] {1,2,3} {4,5,6};

 

二维数组的输入与输出

using System;
class Matrix
{
    static void Main()
    {
        int[,] arr=new int[4,6];
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<6;j++)
            {
                arr[i,j]=(i+1)*10+j+1;
            }        
        }    
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<6;j++)
            {
                Console.Write("{0} ",arr[i,j]);
            }
            Console.WriteLine();
        }
    }
}

运行结果:

 

 

五 命名空间 Namespace

命名空间相当于Java中的 各种 类文件,可以分开来调用。

使用方式

在原有的代码上整体加上

using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            Console.WriteLine("My name is CG");
        }
    }
}

命名空间的调用

同一文件下的调用 namespace.cs

using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            A.PrintName a=new A.PrintName();
            a.intro();
        }
    }
}
namespace A
{
    public class PrintName
    {
        public void intro()
        {
            Console.WriteLine("My name is A");
        }
    }
}

不同文件下的调用

文件1:namespace.cs

using System;
namespace CG
{
    class test
    {
        static void Main()
        {
            PrintName a=new PrintName();
            a.intro();
        }
    }
}

文件2:printname.cs

using System;
namespace CG
{
    public class PrintName
    {
        public void intro()
        {
            Console.WriteLine("My name is A");
        }
    }
}

注意:运行的时候两个文件要同时编译,命令为

csc namespace.cs printname.cs

然后运行主程序namespace.exe

运行结果:

 

 

把类库(方法类)变成 .dll 文件

若改变方法的话直接改变方法类即可,主文件不需改动,也不需要两个同时编译。

把上面的方法文件变成 .dll 文件

 

csc /target:library printname.cs

得到了 printname.dll 文件

把主文件 namespace.cs 与 printname.dll 相关联

 

Csc /reference:printname.dll namespace.cs

最后运行主文件即可

Namespace

运行结果

 

优点:方法改变时不需重复编译主文件,只需更改方法类即可,节省了很多时间。

posted on 2016-08-27 20:35  天梦Interact  阅读(1241)  评论(0编辑  收藏  举报