第六讲 数据类型

using System.Windows;
using System;
using System.Reflection;

namespace test7
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            eslre c = new eslre();
            Type myType = typeof(string); //静态获取Form的数据类型
            PropertyInfo[] pInfos = myType.GetProperties(); //动态获取pInfos的所有属性
            MethodInfo[] mInfos = myType.GetMethods(); //动态获取mInfos的所有方法
            foreach (var p in pInfos)   //遍历打印
            {
                Console.WriteLine(p.Name);
            }
            Console.WriteLine("---------------------------------------------");
            foreach (var m in mInfos)
            {
                Console.WriteLine(m.Name);
            }
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine(myType.FullName);
        }
        

    }

    class eslre
    {
        //public void PrintXTo1(int x)//遍历打印
        //{
        //    for (int i = x; i>=0; i--)
        //    {
        //        Console.WriteLine(i);
        //    }
        //}
        //public void PrintXTo2(int x)//递归打印
        //{
        //    if (x==1)
        //    {
        //        Console.WriteLine(x);
        //    }
        //    else
        //    {
        //        Console.WriteLine(x);
        //        PrintXTo2(x - 1);
        //    }

        //}
    }
}

二内存管理-栈溢出

using System.Windows;
using System;
using System.Reflection;

namespace test7
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            eslre c = new eslre();
            c.BadMethod();
        }
        

    }

    class eslre
    {
        public void BadMethod() //递归栈溢出,每次调用BadMethod都要分配内存给x
        {
            int x = 100;
            this.BadMethod();
        }
    }

    class Program//这代码得把前面的程序都删了
    {
        static unsafe void Main(string[] args)//加unsafe告诉编译器在编写不安全的程序,不然报错
        {
            int* p = stackalloc int[99999999];//通过指针分配超出栈空间的内存,导致栈溢出
        }
    }
}

三堆溢出

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace test8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        List<Window> winList;//定义了一个 List<Window> 类型的字段,用来存放 Window 对象的集合,未实例化
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            winList = new List<Window>();
            for(int i = 0; i < 15000; i++)
            {
                Window w = new Window();//创建一个新的window
                winList.Add(w);//把新的window加进winList集合中
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            winList.Clear();//清空winList集合,垃圾回收器会在合适的时候释放对应的内存,不是立刻
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值