zoukankan      html  css  js  c++  java
  • C#高级编程七十五天----C#使用指针

    C#中使用指针的语法

    假设想在C#中使用指针,首先对项目进行过配置:

     

    看到属性了吗?单击:

     

    看到那个同意不安全代码了吗?选中

    然后将有关指针,地址的操作放在unsafe语句块中.使用unsafekeyword是告诉编译器这里的代码是不安全的.

    unsafekeyword的使用:

    (1)放在函数前,修饰函数,说明在函数内部或函数的形參涉及到指针操作:

    unsafe static void FastCopy(byte[] src, byte[] dst, int count)

    {

        // Unsafe context: can use pointers here.

    }

    不安全上下文的方位从參数列表扩展到方法的结尾,因此指针作为函数的參数时需使用unsafekeyword.

    (2)将有关指针的操作放在由unsafe声明的不安全块中

    unsafe{

    //unsafe context:can use pointers here

    }

    案例:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace Csharp中使用指针

    {

        class Program

        {

            static void Main(string[] args)

            {

                int i = 1;

                unsafe

                {

                    Increment(&i);

                }

                Console.WriteLine(i+" ");

                //演示怎样使用指针操作字符串

                string s = "Code Project is cool";

                Console.WriteLine("the original string : ");

                Console.WriteLine("{0} ",s);

                char[] b = new char[100];

                //将指定书目的字符从此实例中的指定位置拷贝到Unicode字符数组的指定位置

                s.CopyTo(0, b, 0, 20);

                Console.WriteLine("the encode string : ");

                unsafe

                {

                    fixed (char* p = b)

                    {

                        NEncodeDecode(p); 

                    }

                }

                for (int t = 0; t < 10; t++)

                {

                    Console.WriteLine(b[t]);

                }

                Console.WriteLine(" ");

                Console.WriteLine("the decoded string : ");

                unsafe

                {

                    fixed (char* p = b)

                    {

                        NEncodeDecode(p); 

                    }

                }

                for (int t = 0; t < 20; t++)

                    Console.Write(b[t]);

                Console.WriteLine();

                Console.ReadKey();

            }

            unsafe public static void Increment(int* p)

            {

                *p = *p + 1;

            }

            /// <summary>

            /// 将一段字符串通过异或运算进行编码解码的操作.假设您将一段字符串送入这个

            /// 函数,这个字符串会被编码,假设将这一段已经编码的字符送入这个函数,

            /// 这段字符串就会

            /// 解码

            /// </summary>

            /// <param name="s"></param>

            unsafe public static void NEncodeDecode(char* s)

            {

                int w;

                for (int i = 0; i < 20; i++)

                {

                    w = (int)*(s + i);

                    w = w ^ 5;//按位异或

                    *(s + i) = (char)w;

                }

            }

        }

    }

    当中用到了fixed,以下对其进行必要的介绍:

    fixed语句仅仅能出如今不安全的上下文中,C#编译器仅仅同意fixed语句中分配指向托管变量的指针,无法改动在fixed语句中初始化的指针.

    fixed语句禁止垃圾回收器重定位可移动的变量.当你在语句或函数之前使用fixed,你是告诉.net平台的垃圾回收器,在这个语句或函数运行完成前,不得回收其所占用的内存空间.

  • 相关阅读:
    软件测试七年之痒,依然热爱!我还是从前那个少年!
    我想从功能测试转向自动化测试,怎么办?
    python中的一些内置函数
    python中eval()
    集合
    列表的切片:取出来还是一个列表,可用在复制列表元素的操作
    字符串常用的方法
    dict字典,以及字典的一些基本应用
    list列表(也叫数组),以及常用的一些方法
    jsonpath的用法
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6871660.html
Copyright © 2011-2022 走看看