• 指针 PointerPlayground2 示例


    using System;
    
    namespace exercise {
        class Program {
            static unsafe void Main(string[] args) {
                //PointerPlayground2 该示例介绍指针的算术,以及结构指针和类成员指针。开始时,定义一个结构CurrencyStruct,
                //它把货币值表示为美元和美分,再定义一个等价的类CurrencyClass:
    
                //查看存储在栈中的项的地址
                Console.WriteLine("Szie of CurrencyStruct sturct is " + sizeof(CurrencyStruct));
                CurrencyStruct amount1, amount2;
                CurrencyStruct* pAmount = &amount1;
                long* pDollars = &pAmount->Dollars;
                byte* pCents = &(pAmount->Cents);
                Console.WriteLine("Address of amount1 is 0x{0:X}", (uint)&amount1);
                Console.WriteLine("Address of amount2 is 0x{0:X}", (uint)&amount2);
                Console.WriteLine("Address of pAmount is 0x{0:X}", (uint)&pAmount);
                Console.WriteLine("Address of pDollars is 0x{0:X}", (uint)&pAmount);
                Console.WriteLine("Address of pCents is 0x{0:X}", (uint)&pCents);
                pAmount->Dollars = 20;
                *pCents = 50;
                Console.WriteLine("amount1 contains " + amount1);
    
                //查看存储在堆中的项的地址
                Console.WriteLine("\nNow with classes");
                //now try it out with classes
                CurrencyClass amount3 = new CurrencyClass();
                fixed(long* pDollars2 = &amount3.Dollars)
                fixed(byte* pCents2 = &(amount3.Cents)) {
                    Console.WriteLine("amount3,Dollars has address 0x{0:X}", (uint)pDollars2);
                    Console.WriteLine("amount3.Cents has address 0x{0:X}", (uint)pCents2);
                    *pDollars2 = -100;
                    Console.WriteLine("amount3 conteans " + amount3);
                }
            }
        }
    
        internal struct CurrencyStruct {
            public long Dollars;
            public byte Cents;
    
            public override string ToString() {
                return "$" + this.Dollars + "." + this.Cents;
            }
        }
    
        internal class CurrencyClass {
            public long Dollars;
            public byte Cents;
    
            public override string ToString() {
                return "$" + this.Dollars + "." + this.Cents;
            }
        }
    }
  • 相关阅读:
    RabbitMQ消费端自定义监听器DefaultConsumer
    RabbitMQ exchange交换机类型
    RabbitMQ 快速入门
    chrome jsonView插件安装
    谈谈令人头大的prototype 和__proto__
    原生JS写一个淡入淡出轮播图
    模拟聊天对话框
    全选反选的小案例
    原生js做一个简单的进度条
    点击回到顶部的按钮
  • 原文地址:https://www.cnblogs.com/grj1046/p/2854204.html
Copyright © 2020-2023  润新知