예제 #1
0
        public int CompareTo(Book other)
        {
            if (other == null)
            {
                throw new ArgumentNullException($"{nameof(other)} is null");
            }
            int ret;

            ret = string.Compare(Author, other.Author, StringComparison.Ordinal);
            if (ret != 0)
            {
                return(ret);
            }
            ret = string.Compare(Title, other.Title, StringComparison.Ordinal);
            if (ret != 0)
            {
                return(ret);
            }
            ret = PublishingYear.CompareTo(other.PublishingYear);
            if (ret != 0)
            {
                return(ret);
            }
            ret = string.Compare(Genre, other.Genre, StringComparison.Ordinal);
            if (ret != 0)
            {
                return(ret);
            }
            ret = NumberOfPages.CompareTo(other.NumberOfPages);
            return(ret);
        }
예제 #2
0
 /// <summary>
 /// Returns a hash code for this instance.
 /// </summary>
 /// <returns>
 /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
 /// </returns>
 public override int GetHashCode()
 {
     return(Isbn.GetHashCode() ^
            Author.GetHashCode() ^
            Title.GetHashCode() ^
            Publisher.GetHashCode() ^
            PublishingYear.GetHashCode() ^
            PagesNumber.GetHashCode() ^
            Price.GetHashCode());
 }
예제 #3
0
 public override int GetHashCode()
 {
     unchecked {
         const int randomPrime = 397;
         int       hashCode    = Id.GetHashCode();
         hashCode = (hashCode * randomPrime) ^ (Name != null ? Name.GetHashCode() : 0);
         hashCode = (hashCode * randomPrime) ^ (Author != null ? Author.GetHashCode() : 0);
         hashCode = (hashCode * randomPrime) ^ (PublishingYear != null ? PublishingYear.GetHashCode() : 0);
         hashCode = (hashCode * randomPrime) ^ (TimeInterval != null ? TimeInterval.GetHashCode() : 0);
         hashCode = (hashCode * randomPrime) ^ (Language != null ? Language.GetHashCode() : 0);
         return(hashCode);
     }
 }
예제 #4
0
        /// <summary>
        /// Gets a string representation of the <see cref="Book"/> object in <paramref name="format"/>
        /// using <paramref name="formatProvider"/>.
        /// </summary>
        /// <param name="format">A format of string.</param>
        /// <param name="formatProvider">A format provider.</param>
        /// <exception cref="FormatException">
        /// Thrown when <paramref name="format"/> not found in the enum of string formats.
        /// </exception>
        /// <returns>A string representation of the <see cref="Book"/> object in passed format.</returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            StringFormat stringFormat;

            if (!Enum.TryParse <StringFormat>(format.ToUpper(), out stringFormat))
            {
                throw new FormatException("Incorrect string format.");
            }

            if (ReferenceEquals(formatProvider, null))
            {
                formatProvider = CultureInfo.CurrentCulture;
            }

            switch (stringFormat)
            {
            case StringFormat.AT:
            {
                return("Author: " + Author + "\nName: " + Name);
            }

            case StringFormat.ATH:
            {
                return("Author: " + Author + "\nName: " + Name + "\nPublishing house: " + PublishingHouse);
            }

            case StringFormat.ATHY:
            {
                return("Author: " + Author + "\nName: " + Name + "\nPublishing house: " + PublishingHouse + "\nPublishing year: " + PublishingYear.ToString(formatProvider));
            }

            case StringFormat.IATHYN:
            {
                return("ISBN: " + ISBN + "\nAuthor: " + Author + "\nName: " + Name + "\nPublishing house: " + PublishingHouse +
                       "\nPublishing year: " + PublishingYear.ToString(formatProvider) + "\nNumber of pages: " + NumOfPages.ToString(formatProvider));
            }

            case StringFormat.IATHYNP:
            {
                return("ISBN: " + ISBN + "\nAuthor: " + Author + "\nName: " + Name + "\nPublishing house: " + PublishingHouse + "\nPublishing year: " +
                       PublishingYear.ToString(formatProvider) + "\nNumber of pages: " + NumOfPages.ToString(formatProvider) + "\nPrice: " + Price.ToString("C", formatProvider));
            }

            default:
            {
                throw new FormatException("Incorrect string format.");
            }
            }
        }
예제 #5
0
        public override int GetHashCode()
        {
            var hashCode = 785420666;

            hashCode = (hashCode * -1521134295) + EqualityComparer <string> .Default.GetHashCode(_isbn);

            hashCode = (hashCode * -1521134295) + EqualityComparer <string> .Default.GetHashCode(_author);

            hashCode = (hashCode * -1521134295) + EqualityComparer <string> .Default.GetHashCode(_title);

            hashCode = (hashCode * -1521134295) + EqualityComparer <string> .Default.GetHashCode(_publisher);

            hashCode = (hashCode * -1521134295) + _pagesCount.GetHashCode();
            hashCode = (hashCode * -1521134295) + _price.GetHashCode();
            hashCode = (hashCode * -1521134295) + PublishingYear.GetHashCode();
            return(hashCode);
        }