C#基础

#define DEBUG  //c#中#define不用于定义宏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;//使用using来使用命名空间内的实现
using System.IO;    //I/O流文件操作类

namespace CSharpConsoleTest//命名空间可以嵌套,可以使用"."来访问多重嵌套命名空间里的成员
{
    //用户自定义异常类是派生自 ApplicationException 类
    public class TempIsZeroException : ApplicationException
    {
        public TempIsZeroException(string message)
            : base(message)
        {
        }
        public void showInfo()
        {
            Console.WriteLine("TempIsZeroException的异常类的测试信息");
        }
    }
    public interface ITransactions  //C#接口使用方法,接口里定义的函数必须实现
    {
        void showTransaction();
        double getAmount();
    }

    class CTest : ITransactions
    {
        public CTest()  // 构造函数
        {
           Console.WriteLine("对象已创建");
        }
        ~CTest()        //析构函数 析构函数不能继承或重载
        {
           Console.WriteLine("对象已删除");
        }

        public double getAmount()//接口ITransactions.getAmount的实现
        {
            return 1.1;
        }

        public void showTransaction()//接口ITransactions.showTransaction的实现
        {
#if (DEBUG)//预处理器指令的使用方法
            Console.WriteLine("DEBUG is defined");
#else
            Console.WriteLine("DEBUG is not defined");
#endif
            throw (new TempIsZeroException("Zero Temperature found"));//抛出异常方法
        }

        public int i=1;//C#中一个访问修饰符限定一个类成员的范围和可见性
        private char c;
        protected float f;
        internal double length;//成员变量在应用程序内的任何类或方法中可见,相当于全局友元函数
        protected internal int a;//只允许这个成员变量只在他的子类可见,其他地方不可见
        int? num = null;//c#可空类型,可以表示类型范围内所有值,还可以使用null赋值,一般用于不确定的值,输出时为 空
        double[] balance1 = new double[10];//数组建立方法
        double[] balance2 = { 2340.0, 4523.69, 3421.0 };//数据初始化方法1
        public double[] balance3 = new double[5] { 99.1, 98.2, 92.3, 97, 95 };//数组初始化方法2
        public static int sta_num;
        public void swap(ref int x, ref int y)//ref是 引用关键字
        {
            x = 111;
            y = 222;
        }
        public int getValue(out int x)//out表示输出参数,可以使用这一特性输出多个参数
        {
            int temp = 5;
            x = temp;
            return temp;//return只能返回一个参数
        }
    }

    class CFileTest
    {
        public void FileOperator()
        {
            FileStream F = new FileStream("test.txt",FileMode.OpenOrCreate, FileAccess.ReadWrite);

            for (int i = 1; i <= 20; i++)
            {
                F.WriteByte((byte)i);
            }

            F.Position = 0;

            for (int i = 0; i <= 20; i++)
            {
                Console.Write(F.ReadByte() + " ");
            }
            F.Close();
            Console.ReadKey();
        }
    }

    public class DeBugInfo : System.Attribute//自定义特性方法
    {
        private int bugNo;
        private string developer;
        private string lastReview;
        public string message;

        public DeBugInfo(int bg, string dev, string d)
        {
            this.bugNo = bg;
            this.developer = dev;
            this.lastReview = d;
        }
    }
    [DeBugInfo(56, "Weir Wu", "30/11/2016")]//使用特性
    class Program
    {
        public static void Main(string[] args)
        {

            CFileTest file = new CFileTest();
            file.FileOperator();//文件I/O测试
            CTest cls = new CTest();
            int staNum = CTest.sta_num;//静态变量使用方法
            int a1 = cls.i;
            try//异常处理方法
            {
                cls.showTransaction();
            }
            catch (TempIsZeroException e)
            {
                //出现异常执行的语句
                e.showInfo();
            }
            finally
            {
                // 不论是否发生异常都会执行的语句
            }
            int a2 = 200;
            cls.swap(ref a1, ref a2);//传递参数时也需要ref标示
            int a3 = 0;
            cls.getValue(out a3);//调用带有输出参数时也需要out标示
            Console.WriteLine("a1={0}, a2={1}, a3={2}", a1, a2, a3);

            int x = 1;
            ++x;//++和-- 与CPP用法一致
            string num;
            num = Convert.ToString(Console.ReadLine());//从键盘输入
            Console.WriteLine("num={0}", num);
            Console.ReadLine();
            Console.WriteLine("Hello\tWorld\n\n");//使用转义字符
            Console.ReadLine();
            string str = @"hello \\\\t world";//字符串前面加上@,后面的\不会被看成是转义字符标志
            Console.WriteLine("str={0}", str);
            //= += -= *= /= %=>>= <<= &= ^= |= 赋值运算符,三目运算符
            //+ - ! ~ ++ - - (type)* & sizeof 都是从右向左预运算
            foreach (double j in cls.balance3)
            {
                Console.WriteLine("Element[{0}] = {1}", cls.i, j);
                cls.i++;
            }
            Console.ReadKey();

            //C# 结构的特点
            //结构可带有方法、字段、索引、属性、运算符方法和事件。
            //结构可定义构造函数,但不能定义析构函数。但是,您不能为结构定义默认的构造函数。默认的构造函数是自动定义的,且不能被改变。
            //与类不同,结构不能继承其他的结构或类。
            //结构不能作为其他结构或类的基础结构。
            //结构可实现一个或多个接口。
            //结构成员不能指定为 abstract、virtual 或 protected。
            //当您使用 New 操作符创建一个结构对象时,会调用适当的构造函数来创建结构。与类不同,结构可以不使用 New 操作符即可被实例化。
            //如果不使用 New 操作符,只有在所有的字段都被初始化之后,字段才被赋值,对象才被使用。
            while (true) ;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值