C#:如何使用非静态成员访问对象?

C#:如何使用非静态成员访问对象?,c#,object,select,static,C#,Object,Select,Static,我想为图书库创建一个基本系统。我希望每本书都有一个名为“id”的非静态int变量。如何使用变量“id”从类“book”访问对象? 通过访问,我的意思是我想使用“id”来更改其他变量或包含我给它的“id”的对象的访问方法。我也不想使用switch case检查所有声明对象的“id”并返回对该对象的引用。相反,我想知道是否有办法从大小未知的列表中找到正确的对象 这是我的密码: using System; class book { public int id; public int price

我想为图书库创建一个基本系统。我希望每本书都有一个名为“id”的非静态int变量。如何使用变量“id”从类“book”访问对象? 通过访问,我的意思是我想使用“id”来更改其他变量或包含我给它的“id”的对象的访问方法。我也不想使用switch case检查所有声明对象的“id”并返回对该对象的引用。相反,我想知道是否有办法从大小未知的列表中找到正确的对象


这是我的密码:

using System;

class book
{
 public int id;
 public int price;

 public book ( int id, int price )
 {
  this.id = id;
  this.price = price;
 }

 public static book findBook ( int id )
 {
  //what do i put in here?
 }
}

class MainClass
{
 public static void Main( string[] args )
 {
  book b1 = new book( 123, 33 );
  book b2 = new book( 124, 23 );
  book chosenBook;
  int input = Convert.ToInt32( Console.ReadLine() );
  chosenBook = book.findBook( input );
  chosenBook.price = 34;
 }
}


您需要一个对象来管理书籍、数组、列表或任何其他集合。例如:

            book b1 = new book(123, 33);
            book b2 = new book(124, 23);
            var books = new List<book>() { b1, b2 };
            book chosenBook;
            int input = Convert.ToInt32(Console.ReadLine());
            chosenBook = books.FirstOrDefault(b => b.id == input);
            chosenBook.price = 34;
bookb1=新书(123,33);
图书b2=新书(124,23);
var books=newlist(){b1,b2};
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=books.FirstOrDefault(b=>b.id==input);
chosenBook.price=34;
或:

教材
{
公共int id;
公共价格;
公共图书(国际id,国际价格)
{
this.id=id;
这个价格=价格;
}
公共静态列表currentBooks{get;set;}=new List();
公共静态书本findBook(int-id)
{
返回currentBooks.FirstOrDefault(b=>b.id==id);
}
}
类主类
{
公共静态void Main(字符串[]args)
{
b1册=新书(123、33);
图书b2=新书(124,23);
book.currentBooks.Add(b1);
book.currentBooks.Add(b2);
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=book.findBook(输入);
chosenBook.price=34;
}
}
或:

教材
{
公共int id;
公共价格;
公共图书(国际id,国际价格)
{
this.id=id;
这个价格=价格;
}
公共静态词典currentBooks{get;set;}=new Dictionary();
公共静态书本findBook(int-id)
{
账面结果=空;
currentBooks.TryGetValue(id,输出结果);
返回结果;
}
}
类主类
{
公共静态void Main(字符串[]args)
{
b1册=新书(123、33);
图书b2=新书(124,23);
book.currentBooks.Add(b1.id,b1);
book.currentBooks.Add(b2.id,b2);
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=book.findBook(输入);
chosenBook.price=34;
}
}

您需要一个对象来管理书籍、数组、列表或任何其他集合。例如:

            book b1 = new book(123, 33);
            book b2 = new book(124, 23);
            var books = new List<book>() { b1, b2 };
            book chosenBook;
            int input = Convert.ToInt32(Console.ReadLine());
            chosenBook = books.FirstOrDefault(b => b.id == input);
            chosenBook.price = 34;
bookb1=新书(123,33);
图书b2=新书(124,23);
var books=newlist(){b1,b2};
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=books.FirstOrDefault(b=>b.id==input);
chosenBook.price=34;
或:

教材
{
公共int id;
公共价格;
公共图书(国际id,国际价格)
{
this.id=id;
这个价格=价格;
}
公共静态列表currentBooks{get;set;}=new List();
公共静态书本findBook(int-id)
{
返回currentBooks.FirstOrDefault(b=>b.id==id);
}
}
类主类
{
公共静态void Main(字符串[]args)
{
b1册=新书(123、33);
图书b2=新书(124,23);
book.currentBooks.Add(b1);
book.currentBooks.Add(b2);
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=book.findBook(输入);
chosenBook.price=34;
}
}
或:

教材
{
公共int id;
公共价格;
公共图书(国际id,国际价格)
{
this.id=id;
这个价格=价格;
}
公共静态词典currentBooks{get;set;}=new Dictionary();
公共静态书本findBook(int-id)
{
账面结果=空;
currentBooks.TryGetValue(id,输出结果);
返回结果;
}
}
类主类
{
公共静态void Main(字符串[]args)
{
b1册=新书(123、33);
图书b2=新书(124,23);
book.currentBooks.Add(b1.id,b1);
book.currentBooks.Add(b2.id,b2);
书选书;
int input=Convert.ToInt32(Console.ReadLine());
chosenBook=book.findBook(输入);
chosenBook.price=34;
}
}

您希望
findBook()
实际做什么?目前你的“已知书籍”实际上并没有存储在任何地方。在
Main()
方法中只有两个
book
变量。因此,如果你想找到一本具有给定值的书,你必须将该值与这些变量进行比较。你需要将书存储在数组、列表、字典、集合或许多其他选项中。然后您需要进行搜索——这可以在for循环中完成,也可以使用LINQ或其他多种方法之一。实际上,你的问题有很多解决方案&你需要对C语言做一些研究,以找到一些选项。你到底想让
findBook()
做什么?目前你的“已知书籍”实际上并没有存储在任何地方。在
Main()
方法中只有两个
book
变量。因此,如果你想找到一本具有给定值的书,你必须将该值与这些变量进行比较。你需要将书存储在数组、列表、字典、集合或许多其他选项中。然后你需要搜索-这个可以
    class book
    {
        public int id;
        public int price;

        public book(int id, int price)
        {
            this.id = id;
            this.price = price;
        }

        public static Dictionary<int, book> currentBooks { get; set; } = new Dictionary<int, book>();

        public static book findBook(int id)
        {
   book result = null;
            currentBooks .TryGetValue(id, out result);
return result;
        }
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            book b1 = new book(123, 33);
            book b2 = new book(124, 23);
            book.currentBooks.Add(b1.id, b1);
            book.currentBooks.Add(b2.id, b2);
            book chosenBook;
            int input = Convert.ToInt32(Console.ReadLine());
            chosenBook = book.findBook(input);
            chosenBook.price = 34;
        }
    }