成员名称不能与它们的封闭类型相同 [英] member names can not be the same as their enclosing type

查看:135
本文介绍了成员名称不能与它们的封闭类型相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google进行搜索,但答案没有帮助.

希望我能在这里找到解决方案.

错误出现在以下行-public class CartItem : IEquatable<cartitem>

I tried to Google it but the answers were not helpful.

Hope I would find some solution here.

Error comes in the following line -- public class CartItem : IEquatable<cartitem>

public class CartItem
{
    public class CartItem : IEquatable<CartItem>
    {
        #region Properties

        // A place to store the quantity in the cart  
        // This property has an implicit getter and setter.  
        public int Quantity { get; set; }

        private int _productId;
        public int ProductId
        {
            get { return _productId; }
            set
            {
                // To ensure that the Prod object will be re-created  
                _product = null;
                _productId = value;
            }
        }

        private Product _product = null;
        public Product Prod
        {
            get
            {
                // Lazy initialization - the object won''t be created until it is needed  
                if (_product == null)
                {
                    _product = new Product(ProductId);
                }
                return _product;
            }
        }

        public string Description
        {
            get { return Prod.Description; }
        }

        public decimal UnitPrice
        {
            get { return Prod.Price; }
        }

        public decimal TotalPrice
        {
            get { return UnitPrice * Quantity; }
        }

        #endregion

        // CartItem constructor just needs a productId  
        public CartItem(int productId)
        {
            this.ProductId = productId;
        }

        /** 
         * Equals() - Needed to implement the IEquatable interface 
         *    Tests whether or not this item is equal to the parameter 
         *    This method is called by the Contains() method in the List class 
         *    We used this Contains() method in the ShoppingCart AddItem() method 
         */
        public bool Equals(CartItem item)
        {
            return item.ProductId == this.ProductId;
        }

        #region IEquatable<CartItem> Members

        bool IEquatable<CartItem>.Equals(CartItem other)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

推荐答案

您不能嵌套具有相同名称的类,因此请更改内部类的名称.

另外,由于要使用泛型定义内部类并参考IMO的CartItem,因此应将它们保留为单独的类.
You cannot nest classes with the same name, so change for example the inner class name.

Also since you''re defining the inner class with generics and refer to CartItem IMO you should keep these as separate classes.


这篇关于成员名称不能与它们的封闭类型相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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