写代码的麒麟臂 2015-12-16 06:45 采纳率: 50%
浏览 1392
已采纳

我想问下最后主函数里的lib[i] = b是什么意思?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Book
{
private string bid;
private string bname;
public string Bid
{
get { return bid; }
set { bid = value; }
}

    public string Bname
    {
        get { return bname; }
        set { bname = value; }
    }
}
class Library
{
    Book[] book;

    public Library(int len)
    {
        book = new Book[len];
    }
    public Book this[int idx]
    {
        get
        {
            return book[idx];
        }
        set
        {
            book[idx] = value;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        int length;
        Console.Write("图书馆容量:");
        length = Convert.ToInt32(Console.ReadLine());
        Library lib = new Library(length);
        for (int i = 0; i < length; i++)
        {
            Book b = new Book();
            Console.Write("编号{0}:", i + 1);
            b.Bid = Console.ReadLine();
            Console.Write("书名{0}:", i + 1);
            b.Bname = Console.ReadLine();
            lib[i] = b;
        }
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine(lib[i].Bid + " " + lib[i].Bname);
        }
    }
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-12-16 06:50
    关注

    这个程序应该是一个习作,目的是练习使用C#的索引器(Indexer)语法。
    lib[i] = b;的作用是调用Library定义的那个索引器的set方法,把Book对象b传给Book[]数组。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题