• 不错的quiz


    using System;
    using System.Collections.Generic;

    namespace abcd
    {
        
    class Program
        
    {
            S s;
            
    struct S
            
    {
                
    public int x; //public int x = 0 编译错误?解释为什么
            }


            C c 
    = new C();
            
    class C
            
    {
                
    public int x = 0;
            }
            
            
            
    void print()
            
    {
                Console.WriteLine(
    "{0}\t{1}", s.x, c.x);
            }


            
    void Foo1(S s, C c)
            
    {
                s.x
    ++;
                c.x
    ++;
                print();
            }


            
    void Foo2(S s, C c)
            
    {
                s 
    = new S();
                c 
    = new C();
                print();
            }


            
    void Foo3(ref S s, ref C c)
            
    {
                s.x
    ++;
                c.x
    ++;
                print();
            }


            
    void Foo4(ref S s, ref C c)
            
    {
                s 
    = new S();
                c 
    = new C();
                print();
            }
           

            
    void Test()
            
    {
                Foo1(s, c);
                Foo2(s, c);
                Foo3(
    ref s, ref c);
                Foo4(
    ref s, ref c);
                
    // 输出结果,解释为什么:
                
    // 0    1
                
    // 0    1
                
    // 1    2
                
    // 0    0

                List
    <S> l1 = new List<S>();
                l1.Add(s);
                
    //l1[0].x++; 编译错误?解释为什么

                S[] l 
    = new S[1];
                l[
    0= new S();
                l[
    0].x++;
                
    // 数组OK,解释为什么

                List
    <C> l2 = new List<C>();
                l2.Add(c);
                l2[
    0].x++;
                
    // OK,解释为什么
            }


            
    static void Main()
            
    {
                
    new Program().Test();
            }
           
        }

    }


    解释每一个为什么,并且画出对象在内存中的布局变化图!
    欢迎大家阅读我的极客时间专栏《Java业务开发常见错误100例》【全面避坑+最佳实践=健壮代码】
  • 相关阅读:
    STM32.ADC
    电源方案集
    什么叫二级域名
    android驱动学习---led实验
    Get,Post和Head具体解释
    Android 编码规范
    VC:当前不会命中断点,还没有为该文档载入不论什么符号
    经常使用的结构体
    【Facebook的UI开发框架React入门之九】button简单介绍(iOS平台)-goodmao
    记录遇到的ios下的bugs[废弃]
  • 原文地址:https://www.cnblogs.com/lovecherry/p/1136624.html
Copyright © 2020-2023  润新知