net/c#_8
TRANSCRIPT
![Page 1: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/1.jpg)
Платформата Microsoft .NET иезикът на програмиране C#
![Page 2: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/2.jpg)
Индексатори и свойства
![Page 3: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/3.jpg)
Достъпване на полета на класа
директен публичен достъп get / set методи свойства обектно ориентирано
![Page 4: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/4.jpg)
Директен публичен достъп
public class Book {public decimal price;
}
Book book1 = new Book();book1.price = 12.34M;Console.WriteLine(book1.price);
![Page 5: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/5.jpg)
get / set методи
public class Book {private decimal price;public void SetPrice(decimal price) {this.price = price;} public decimal GetPrice() {return price;}
}
Book book1 = new Book();book1.SetPrice(12.34M);Console.WriteLine(book1.GetPrice());
![Page 6: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/6.jpg)
Свойства
public class Book {private decimal price;public decimal Price {set {this.price = price;}get {return price;}}
}Book book1 = new Book();book1.Price = 12.34M;Console.WriteLine(book1.Price);
![Page 7: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/7.jpg)
Oбектно ориентирано
public class Book {private decimal price;public Book(decimal price) {this.price = price;} public decimal CalculateTotalPrice(int books) {return price * books;}
}
Book book1 = new Book(12.34M);Console.WriteLine(book1.CalculateTotalPrice(1));
![Page 8: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/8.jpg)
Автоматично генериране на свойства
public class Book {public decimal Price {set; get;}
}Book book1 = new Book();book1.Price = 12.34M;Console.WriteLine(book1.Price);
![Page 9: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/9.jpg)
Инициализиране
public class Book {public decimal Price {set; get;} = 3.14М;
}Book book1 = new Book();Console.WriteLine(book1.Price);
![Page 10: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/10.jpg)
Индексатори
Позволяват използването на клас подобно на масив с [ ]
![Page 11: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/11.jpg)
Пример за индексаторpublic class Bookshelf {
private Book[] books = new Book[10];public Book this[int number] {get {if (number >= 0 && number < books.Length) {return books[number]; }throw new ArgumentException(“Not a valid bookshelf position");}set {if (number >= 0 && number < books.Length) { books[number] = value; } else {throw new ArgumentException(“Not a valid bookshelf position");}}
}}
![Page 12: NET/C#_8](https://reader035.vdocuments.site/reader035/viewer/2022070516/587387191a28ab272d8b5f4d/html5/thumbnails/12.jpg)
Пример за индексаторBookshelf bookshelf1 = new Bookshelf();bookshelf1[2] = new Book(12.34M);bookshelf1[12] = new Book(22.34M);Console.WriteLine(bookshelf1[2].CalculateTotalPrice(1));