[询问]-结构 [英] [ASK] - Struct

查看:65
本文介绍了[询问]-结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家们,我是C#的新手.我想问一下C#中的struct.我对此有一个疑问:

Hi experts, i''m a newbie in C#. I wanna ask u about struct in C#. I''ve a problem about this one :

class Program
  {
    static void Main(string[] args)
    {
      Add();
      Multiply();
      Console.ReadLine();
    }
    private static void Add()
    {
      S sd = new S();
      sd.data1.Add(0, 3);
      sd.data1.Add(1, 5);
      sd.bil1 = sd.data1[0];
      sd.bil2 = sd.data1[1];
      int hasil = sd.bil1 + sd.bil2;
      Console.WriteLine("Hasil penjumlahan : {0}", hasil);
    }
    private static void Multiply()
    {
      S sd = new S();
      sd.data1.Add(0, 6);
      sd.data1.Add(1, 2);
      sd.bil1 = sd.data1[0];
      sd.bil2 = sd.data1[1];
      int hasil = sd.bil1 * sd.bil2;
      Console.WriteLine("Hasil perkalian : {0}", hasil);
    }
    struct S
    {
      public IDictionary<int, int> data1 { get; set; }
      public int bil1;
      public int bil2;
    }
  }



发生错误:(,您是否会根据上述问题告诉我如何在良好的编程中使用struct .. :(不是基于我的错误.
因为,我真的很想了解struct.谢谢..:)



There is an error occured :( , would u tell me how to use struct in good programming based on my problem above.. :( not based on my mistake.
Cause, i''m really want to understand about struct.. but in a good programming. Thank you.. :)

推荐答案

该错误消息通常可以提供导致异常原因的线索.

对象引用未设置为对象的实例"
The error message normally gives a clue to the reason for the exception.

"Object reference not set to an instance of an object"


当您使用结构S中的data1时,您必须实现ur
接口变量.

根据您的代码
当您使用

"sd.data1.Add(0,3);"
异常的消息是SHOWN
对象引用未设置为对象的实例."

因此您必须在每次使用时实现struct S的每个接口变量
的data1 data1
就像

S sd = new S();

sd.data1 =新字典< int,int>();

sd.data1.Add(0,3);



有关所有变量的此类信息
when u use data1 from struct S u must implement ur
interface variable .

acoording to ur code
when u use

"sd.data1.Add(0, 3);"
an exception messqage is SHOWN
"Object reference not set to an instance of an object."

so u must implement every interface variable of struct S at every use
of data1 data1
like

S sd = new S();

sd.data1=new Dictionary<int,int>();

sd.data1.Add(0, 3);



an so on for all variables


请访问以下链接,以更好地了解结构体

http://msdn.microsoft.com/en-us/library/ms173109.aspx [ ^ ]


http://msdn.microsoft.com/en-us/library/0taef578.aspx [ ^ ]
请让我知道您是否真的想了解更多...:)
Please, go to the following link to have a better understanding of structs

http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]


http://msdn.microsoft.com/en-us/library/0taef578.aspx[^]
Please, let me know if you really want to know more... :)


这篇关于[询问]-结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆