zoukankan      html  css  js  c++  java
  • WeakReference 弱引用

    弱引用是使用WeakReference类创建的.因为对象可能在任意时刻被回收,所以在引用该对象前必须确认它存在.

     1 class MainEntryPoint
     2 {
     3 static void Main()
     4 {
     5 // Instantiate a weak reference to MathTest object
     6 WeakReference mathReference = new WeakReference(new MathTest()); 
     7 MathTest math;
     8 if(mathReference.IsAlive)
     9 {
    10 math = mathReference.Target as MathTest;
    11 math.Value = 30;
    12 Console.WriteLine(
    13 "Value field of math variable contains " + math.Value);
    14 Console.WriteLine("Square of 30 is " + math.GetSquare());
    15 
    16 }
    17 else
    18 {
    19 Console.WriteLine("Reference is not available.");
    20 }
    21 
    22 GC.Collect();
    23 
    24 if(mathReference.IsAlive)
    25 {
    26 math = mathReference.Target as MathTest;
    27 }
    28 else
    29 {
    30 Console.WriteLine("Reference is not available.");
    31 }
    32 }
    33 }
    34 
    35 // Define a class named MathTest on which we will call a method
    36 class MathTest
    37 {
    38 public int Value;
    39 
    40 public int GetSquare()
    41 {
    42 return Value*Value;
    43 }
    44 
    45 public static int GetSquareOf(int x)
    46 {
    47 return x*x;
    48 }
    49 
    50 public static double GetPi()
    51 {
    52 return 3.14159;
    53 }
    54 }
    View Code
  • 相关阅读:
    flex兼容写法
    多行文字,最后一行省略号(适用于移动端)
    checkbox样式修改
    响应式布局
    微信常用的页面跳转
    css小技巧(清除滚动条)
    JS学习---PHP浅识
    qml 画页迁移
    list滚动条Scroll 偏移和长度计算公式总结
    qml listview关键字高亮
  • 原文地址:https://www.cnblogs.com/farmer-y/p/5938661.html
Copyright © 2011-2022 走看看