Click here to Skip to main content
15,910,234 members
Home / Discussions / C#
   

C#

 
GeneralPointers Pin
Roger H Art29-Mar-04 9:32
sussRoger H Art29-Mar-04 9:32 
GeneralRe: Pointers Pin
Judah Gabriel Himango29-Mar-04 9:52
sponsorJudah Gabriel Himango29-Mar-04 9:52 
GeneralRe: Pointers Pin
Roger H Art29-Mar-04 9:59
sussRoger H Art29-Mar-04 9:59 
GeneralRe: Pointers Pin
Judah Gabriel Himango29-Mar-04 11:23
sponsorJudah Gabriel Himango29-Mar-04 11:23 
GeneralRe: Pointers Pin
Roger H Art29-Mar-04 12:13
sussRoger H Art29-Mar-04 12:13 
GeneralRe: Pointers Pin
Roger H Art29-Mar-04 12:23
sussRoger H Art29-Mar-04 12:23 
GeneralRe: Pointers Pin
Andy Brummer29-Mar-04 16:09
sitebuilderAndy Brummer29-Mar-04 16:09 
GeneralRe: Pointers Pin
Heath Stewart29-Mar-04 17:39
protectorHeath Stewart29-Mar-04 17:39 
Why do you need to use an unsafe context for this? mArr is your "pointer" to the array of bytes, except that in the managed code world it's called references, which can be moved around by the garbage collector (GC) in the heap whenever it needs to optimize memory (which is why you must use the fixed keyword, or the GCHandle class to lock the object on the heap for that context). Arrays are also reference types, so you don't need to use ref or out with them except in some uncommon cases when you need to declare an undimensioned array and have a method (not "function") return it.

The code can simply be:
byte[] mArr = new byte[] {6, 9, 11, 25};
for (int i=0; i<mArr.Length; i++)
  Console.WriteLine("0x{0:x2}", mArr[i]);
Much easier, and correct (since unsafe contexts cause security problems in non-local deployments and are not completely managed by the CLR).

Even if this is a field in your class, it doesn't matter:
public class MyClass
{
  // You should actually use properties to expose publicly, not fields.
  public byte[] bytes;
  public MyClass()
  {
    // Can just as easily come from anywhere.
    bytes = new byte[] {6, 9, 11, 25}; 
  }
}
Your declaration of the byte[] array doesn't dimension it; it only declares an array of a certain type. In unmanaged code, the same is true, since a char* points to the first element (the pointer to the array itself) of a char[], for example.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Pointers Pin
Roger H Art30-Mar-04 1:18
sussRoger H Art30-Mar-04 1:18 
GeneralRe: Pointers Pin
Heath Stewart30-Mar-04 3:37
protectorHeath Stewart30-Mar-04 3:37 
GeneralRe: Pointers Pin
Roger H Art30-Mar-04 6:22
sussRoger H Art30-Mar-04 6:22 
GeneralRe: Pointers Pin
Heath Stewart30-Mar-04 8:27
protectorHeath Stewart30-Mar-04 8:27 
GeneralNeed Help with Writing a IO.Stream Pin
David Flores29-Mar-04 8:32
David Flores29-Mar-04 8:32 
GeneralRe: Need Help with Writing a IO.Stream Pin
Heath Stewart29-Mar-04 8:40
protectorHeath Stewart29-Mar-04 8:40 
GeneralAccess files in use Pin
Shaun Becker29-Mar-04 7:33
Shaun Becker29-Mar-04 7:33 
GeneralRe: Access files in use Pin
Heath Stewart29-Mar-04 8:23
protectorHeath Stewart29-Mar-04 8:23 
GeneralPolymorphism and types Pin
esuyer4saic29-Mar-04 7:30
esuyer4saic29-Mar-04 7:30 
GeneralRe: Polymorphism and types Pin
Heath Stewart29-Mar-04 8:18
protectorHeath Stewart29-Mar-04 8:18 
GeneralReporting Pin
rags29-Mar-04 6:35
rags29-Mar-04 6:35 
GeneralRe: Reporting Pin
Heath Stewart29-Mar-04 8:09
protectorHeath Stewart29-Mar-04 8:09 
GeneralRe: Reporting Pin
Michael Flanakin29-Mar-04 17:25
Michael Flanakin29-Mar-04 17:25 
GeneralCustom Install Pin
dotnetdev@univ.kiev.ua29-Mar-04 6:05
dotnetdev@univ.kiev.ua29-Mar-04 6:05 
GeneralRe: Custom Install Pin
Heath Stewart29-Mar-04 8:07
protectorHeath Stewart29-Mar-04 8:07 
GeneralRe: Custom Install Pin
dotnetdev@univ.kiev.ua29-Mar-04 22:10
dotnetdev@univ.kiev.ua29-Mar-04 22:10 
GeneralRe: Custom Install Pin
Heath Stewart30-Mar-04 3:28
protectorHeath Stewart30-Mar-04 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.