学习C#的笔记

来源:互联网 发布:ubuntu 不显示标题栏 编辑:程序博客网 时间:2024/04/30 00:45

1.打开命令提示符并切换到存 *.cs 的目录。敲入以下命令:
csc *.cs

*.cs 被编译并链接成*.exe

稍为想象一下第一个应用程序和一个编译器开关的使用:
csc
/out:hello.exe helloworld.cs
这个开关告诉编译器输出文件命名为hello.exe

/'''' 单引号
/"
双引号
//
反斜杠
/0
空字符
/a
感叹号(Alert
/b
退格
/f
换页
/n
新行
/r
回车
/t
水平 tab
/v
垂直tab

2. string[] arrLanguages = { "C", "C++", "C#" };
该简写效果等同以下:
arrLanguages[0]="C"; arrLanguages[1]="C++"; arrLanguages[2]="C#";

 

int[,] arr = {{0,1}, {2,3}, {4,5}};
它是以下的简写:
arr[0,0] = 0; arr[0,1] = 1;
arr[1,0] = 2; arr[1,1] = 3;
arr[2,0] = 4; arr[2,1] = 5;

31)调用方法:(开方)

using System;

public class SquareSample

{

    public int calcSquare(int nSideLength)

    {

        return nSideLength * nSideLength;

    }

}

class SquareApp

{

    public static void Main()

    {

        SquareSample sq = new SquareSample();

        Console.WriteLine(sq.calcSquare(25).ToString());

    }

}

 

2). 引用参数(平方)

using System;

public class squaresample

{

    public void CalcSquare(ref int nOne4All)

    {

        nOne4All *= nOne4All;

    }

}

class squareapp

{

    public static void Main()

    {

        squaresample sq = new squaresample();

        int wx = 20;//此处须初使化:

        sq.CalcSquare(ref wx);

        Console.WriteLine(wx.ToString());

    }

}

3).定义一个输出参数:

using System;

public class squaresample

{

    public void CalcSquare(int nsidelength, out int nquared)

    {

        nquared = nsidelength * nsidelength;

    }

}

class squareapp

{

    public static void Main()

    {

        squaresample sq = new squaresample();

        int nsquared;  //此处不用初使化:

        sq.CalcSquare(15,out nsquared);

        Console.WriteLine(nsquared.ToString());

    }

}

4). 改写方法
面向对象设计的重要原则就是多态性。不要理会高深的理论,多态性意味着:当基类程序员已设计好用于改写的方法时,在派生类中,你就可以重定义(改写)基类的方法。基类程序员可以用virtual 关键字设计方法:virtual void CanBOverridden()
当从基类派生时,所有你要做的就是在新方法中加入override关键字:
override void CanBOverridden()

(1). class RightAngledTriangle : Triangle
在类语句中冒号(:)表示RightAngledTriangle从类 Triangle派生。那就是你所必须要做的

例:一不正规三角形和一个直角三角形的面积计算:

using System;

 

class Triangle

{

 public virtual double ComputeArea(int a, int b, int c)

 {

 // Heronian formula

double s = (a + b + c) / 2.0;

 double dArea = Math.Sqrt(s*(s-a)*(s-b)*(s-c));

 return dArea;

 }

 }

 

 class RightAngledTriangle:Triangle

 {

 public override double ComputeArea(int a, int b, int c)

 {

 double dArea = a*b/2.0;

 return dArea;

 }

}

 

 class TriangleTestApp

 {

 public static void Main()

 {

 Triangle tri = new Triangle();

 Console.WriteLine(tri.ComputeArea(2, 5, 6));

 

 RightAngledTriangle rat = new RightAngledTriangle();

 Console.WriteLine(rat.ComputeArea(3, 4, 5));

 }

 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



4Char str=str[0];    取出字符串中的第一个字符;

5

System.IO.StreamReader file1=System.IO.File.AppendAllText(string path,string content,enconding);

将指定的字符串追加到文件中,若文件不存在则创建该文件

System.IO.StreamReader file1=System.IO.File. AppendTextstring,path;将指定文本添加到文件中,

System.IO.StreamReader file1=System.IO.File.Copstring sourcefilename,string directfilename,booloverwite);将现有文件复制到新文件,

System.IO.StreamReader file1=System.IO.File.Create(string path,int bufersize,IO.Options Options, System.Security.AccessControl.FileSecurity FileSecurity);

创制或改写具有指定缓冲大小,文件选项和文件安全性的文件,

System.IO.StreamReader file1=System.IO.File.CreateText(string path);创建或打开一个文本文件

System.IO.StreamReader file1=System.IO.File.Delete(string path);删除指定文件,若不存在,不引发异常

System.IO.StreamReader file1=System.IO.File.Encrypt(string path);加密一个文件

System.IO.StreamReader file1=System.IO.File.Decrypt(string path);解密一个文件

System.IO.StreamReader file1=System.IO.File.Exists(string path);确定指定的文件是否存在

System.IO.StreamReader file1=System.IO.File.GetAttributes(string path);获取指定路径文件的属性

System.IO.StreamReader file1=System.IO.File.GetCreationTime();创建日期或时间

System.IO.StreamReader file1=System.IO.File.GetAccessTime();访问时间

System.IO.StreamReader file1=System.IO.File.GetWriteTime();写入时间

System.IO.StreamReader file1=System.IO.File.Move(string sourcefilename,string directfilename);将指定文件移动到指定位置

System.IO.StreamReader file1=System.IO.File.Open(Path,IO.file.mode,IO.FileAccess fileaccess,IO.Fileshare fileshare);

System.IO.StreamReader file1=System.IO.File.OpenRead();以只读格式读取

System.IO.StreamReader file1=System.IO.File.OpenText();以文本格式读取

System.IO.StreamReader file1=System.IO.File.OpenWrite();打开现有文件以进行写入,

System.IO.StreamReader file1=System.IO.File.ReadAllBytes();打开一个文件,读出一字符串,然后关

System.IO.StreamReader file1=System.IO.File.ReadAllLines();读取所有行,关闭文件

System.IO.StreamReader file1=System.IO.File.ReadAllText();读取所有行,关闭文件

System.IO.StreamReader file1=System.IO.File.Replace(string sourcefilename,string directfilename bool ignoreMetaDataErrors);用其它文件内容替换指定文件内容,删除原始文件,并创建文件被替换文件的备份和(可选)忽略和并错误

System.IO.StreamReader file1=System.IO.File.WriteAllBytes(string path,byte[] bytes);创建一新文件,写入指定的字节数组,关闭文件,若存在,则改写该文件。

System.IO.StreamReader file1=System.IO.File.WriteAllLines(string path,string[]content,enconding);创建一新文件,写入指定字符串数组,关闭,若存在,则改写原文件。

System.IO.StreamReader file1=System.IO.File.WriteAllText(string path,string content,enconding); 创建一新文件,写入指定字符串数组,关闭,若存在,则改写原文件.

System.IO.StreamReader file1=System.IO.File.SetAccessControl();

System.IO.StreamReader file1=System.IO.File.Set Attributes();

System.IO.StreamReader file1=System.IO.File.SetCreettime();

System.IO.StreamReader file1=System.IO.File. SetLastAccessTime();

System.IO.StreamReader file1=System.IO.File.SetLastWriteTime();

 

原创粉丝点击