2013-08-02 33 views
-1

究竟这意味着在C#中的数据类型*表示

数据类型*

:INT *,双*,字符*,...

任何一个可以给它一些解释请。

在此先感谢。

+2

这是C/C++,不C#的。这是一个指针:http://www.cs.cf.ac.uk/Dave/C/node10.html – GrandMasterFlush

+0

下面是在C#中的数据类型:http://msdn.microsoft.com/en-us/library/ms173104 (v = VS.80).ASPX – Max

+1

@GrandMasterFlush眼罩离:) – sehe

回答

1

这是一个不安全的指针。 Unsafe Code Tutorial

下面是使用它的一个例子:How to pull out alpha and count digits using regex?

private static unsafe List<long> ParseNumbers(char[] input) 
{ 
    var r = new List<long>(); 

    fixed (char* begin = input) 
    { 
     char* it = begin, end = begin + input.Length; 

     while (true) 
     { 
      while (it != end && (*it < '0' || *it > '9')) 
       ++it; 

      if (it == end) break; 

      long accum = 0; 
      while (it != end && *it >= '0' && *it <= '9') 
       accum = accum * 10 + (*(it++) - '0'); 

      r.Add(accum); 
     } 
    } 

    return r; 
} 
0

那些是Pointer types

在不安全的上下文中,类型可能是指针类型以及值类型或引用类型。指针类型声明采用以下形式之一:

type* identifier; 
void* identifier; //allowed but not recommended 
1

看一看Pointer types (C# Programming Guide)

在不安全上下文中,一个类型可以是指针类型,值类型,或 参考类型。指针类型声明采用以下格式之一:

type * identifier;

void * identifier; //允许但不建议

0

它们被称为Pointer types

在不安全上下文中,一个类型可以是指针型以及一​​个 值类型或引用类型。但是,指针类型也可能是 ,用于不安全上下文之外的typeof表达式中,因为此类用法并非不安全。

指针型被写入作为非托管型或关键字void, 后跟一个*标记:

在指针类型的*之前指定的类型被称为指针的 所指类型类型。它表示指针类型的值所指向的 变量的类型。

与引用(引用类型的值)不同,指针不是由垃圾收集器跟踪的 - 垃圾收集器没有指针和指向的数据的 知识。对于这个 原因,指针不允许指向包含引用的引用或 结构,并且指针 的引用类型必须是非托管类型。

0

这是指针在C#

请花时间阅读本Unsafe Code Tutorial

using System; 

class Test 
{ 
    // The unsafe keyword allows pointers to be used within 
    // the following method: 
    static unsafe void Copy(byte[] src, int srcIndex, 
     byte[] dst, int dstIndex, int count) 
    { 
     if (src == null || srcIndex < 0 || 
      dst == null || dstIndex < 0 || count < 0) 
     { 
      throw new ArgumentException(); 
     } 
     int srcLen = src.Length; 
     int dstLen = dst.Length; 
     if (srcLen - srcIndex < count || 
      dstLen - dstIndex < count) 
     { 
      throw new ArgumentException(); 
     } 


      // The following fixed statement pins the location of 
      // the src and dst objects in memory so that they will 
      // not be moved by garbage collection.   
      fixed (byte* pSrc = src, pDst = dst) 
      { 
        byte* ps = pSrc; 
        byte* pd = pDst; 

      // Loop over the count in blocks of 4 bytes, copying an 
      // integer (4 bytes) at a time: 
      for (int n =0 ; n < count/4 ; n++) 
      { 
       *((int*)pd) = *((int*)ps); 
       pd += 4; 
       ps += 4; 
      } 

      // Complete the copy by moving any bytes that weren't 
      // moved in blocks of 4: 
      for (int n =0; n < count%4; n++) 
      { 
       *pd = *ps; 
       pd++; 
       ps++; 
      } 
      } 
    } 


    static void Main(string[] args) 
    { 
     byte[] a = new byte[100]; 
     byte[] b = new byte[100]; 
     for(int i=0; i<100; ++i) 
      a[i] = (byte)i; 
     Copy(a, 0, b, 0, 100); 
     Console.WriteLine("The first 10 elements are:"); 
     for(int i=0; i<10; ++i) 
      Console.Write(b[i] + " "); 
     Console.WriteLine("\n"); 
    } 
} 

和输出

The first 10 elements are: 
0 1 2 3 4 5 6 7 8 9 

我的牙齿这会给你一个想法,以低调的指针在C#以及如何使用它

好运