qq_25255553 2016-07-26 06:49 采纳率: 33.3%
浏览 2334
已采纳

C# 多层嵌套里的变量怎么赋值

图片说明
Respose response = new Respose();
response.body.authentication.useraccount = "18857475598";这样赋值赋不进去,提示未将对象引用设置到对象的实例。 本人新手,求解应该怎么赋值才能赋值进去

  • 写回答

4条回答 默认 最新

  • LyphardMelody丶 2016-07-26 10:02
    关注

    给对象就行了
    static void Main(string[] args)
    {
    Respose respose = new Respose();
    respose.Body = new Body()
    {
    Authentication = new Authentication()
    {
    Useraccount = "aaaa"
    }
    };
    Console.WriteLine(respose.Body.Authentication.Useraccount);
    }

    不这样你就一个一个new出来赋值再添加。

    static void Main(string[] args)
    {
    Respose respose = new Respose();
    Body body = new Body();
    Authentication authentication = new Authentication();

            authentication.Useraccount = "aaa";
            body.Authentication = authentication;
            respose.Body = body;
    
            Console.WriteLine(respose.Body.Authentication.Useraccount);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?