C# 语言中的其他成分

一 编译预处理

1 标识符声明

① #define定义一个标识符;
② #undef 取消定 一个标识符;

2 条件处理

#if #elif #else #endif

3 信息报告

#error和#warning

4 行号标记

#line 行号“文件名”

二 unsafe及指针

1 unsafe

用于修饰类、方法等

2 fixed及指针

fixed(类型*指针名=表达式)语句;

3 sizeof运算符

sizeof(简单或结构类型名)

4 stackalloc

在栈上分配的内存,而不是在堆上,因此不会担心内存被垃圾回收器自动回收;

5 指针示例

class FileStream:Stream
{
    int handle;

    [dllimport("kernel32",SetLastError=true)]
    static extern unsafe bool ReadFile(int hFile,
    void*lpBuffer,int nBytesToRead,
    int* nBytesRead,Overlapped*lpOverlapped);

    public unsafe int Read(byte[] buffer,int index,int count)
    {
        int n=0;
        fixed(byte*p=buffer)
        {
            ReadFile(handle,p+index,count,&n,null);
        }
        return n;
    }
}

指针

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 指针
{
   class Test
    {
        static unsafe void Copy(byte[] src, byte[] dst, int count)
        {
            int srcLen = src.Length;
            int desLen = dst.Length;
            if(srcLen<count||desLen<count)
            {
                throw new ArgumentException();
            }

            fixed(byte* pSrc=src,pDst=dst)
            {
                byte* ps = pSrc;
                byte* pd = pDst;
                for(int n=0;n<count;n++)
                {
                    *pd++ = *ps++;
                }
            }
        }

        static void Main()
        {
            byte[] a = new byte[100];
            byte[] b= new byte[100];
            for (int i = 0; i < 100; ++i)
                a[i] = (byte)i;

            Copy(a, b, 100);

            Console.WriteLine("The first 10 elements are:");

            for (int i = 0; i < 10; ++i)
                Console.Write(b[i] + "{0}", i < 9 ? " " : " ");

            Console.WriteLine("\n");
            Console.ReadKey();
        }
    }
}

stackalloc分配内存空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 分配内存空间
{
    class Test
    {
        unsafe static string IntToString(int value)
        {
            char* buffer = stackalloc char[16];
            char* p = buffer + 16;
            int n = value >= 0 ? value : -value;
            do
            {
                *--p = (char)(n % 10 + '0');
                n /= 10;
            } while (n != 0);
            if (value < 0) *--p = '-';


            return new string(p, 0, (int)(buffer + 16 - p));
        }

        static void Main()
        {
            Console.WriteLine(IntToString(12345));
            Console.WriteLine(IntToString(-999));

            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值