• 不错的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例》【全面避坑+最佳实践=健壮代码】
  • 相关阅读:
    IIS7.x经典模式与集成模式
    pocketsphinx实现连续大词汇量语音识别
    js对象冒充实现的继承
    你不得不知道的HTML5的新型标签
    (译)开发优秀的虚拟现实体验:从开发I Expect You to Die中总结的六个要点
    《构建之法》阅读梳理篇读后感
    VR介绍
    推荐
    VR设备
    开发VR游戏的基本要求
  • 原文地址:https://www.cnblogs.com/lovecherry/p/1136624.html
Copyright © 2020-2023  润新知