Attribute:
1,The attribute can mark any access modifier and can be defined in the interface;
2,The attribute type cannot be void;
3,The get accessors of the attribute do not accept parameters;
4,C #Generic attributes are not allowed;
5,The accessibility of the accessors in the property is the same as that of the property by default, and can also be limited by itself;
Public Class Sometype
{
Public StringName
{
Get{Return Null;}
Protected Set{}
}
}
Difference between attributes and fields:
1,Attributes are similar to fields, but attributes are methods;
2,Attribute methods also throw an exception, and field access will never throw an exception;
3,The attribute cannot be passed to the method as an out or ref parameter, and the field can;
Indexer:
Indexer is a special type of attribute. With these attributes, You can reference your own classes as you reference arrays. Obviously, this function is particularly useful when creating collection classes. In some other cases, such as processing large files or abstracting some limited resources, it is of course very useful to make classes behave like arrays.
1,The indexer has at least one parameter or multiple parameters;
2,Parameters and return types can be any data type (except void );
3,C #Only the indexer can be defined on the instance of the object;
4,Indexer with the same parameter set is not allowed;
Public Accessable_test This[IntI]
{
Get{ReturnAccesses [I];}
Set{Accesses [I] =Value;}
}
Public Accessable_test This[StringSTR]
{
Get{Return Null;}
Set{}
}
// Error: A member with the same parameter type has been defined.
// Public String This [int I]
//{
//Get {return NULL ;}
//Set {}
//}
Differences between attributes and indexer:
1Each attribute of the class must have a unique name, and each indexer defined in the class must have a unique signature) or the parameter list (so that the indexer can be reloaded ).
2The property can be static, and The indexer must be an instance Member.
Public Class Accessable_test
{
List<Accessable_test> Accesses =New List<Accessable_test> ();
Private IntM_id;
Public IntID
{
Get{ReturnM_id ;}
Set{M_id =Value;}
}
// Private string m_str;
// Error:Id definition already included
// Public String ID
//{
//Get {return m_str ;}
//}
Private StringM_name;
Public StringName
{
Get{ReturnM_name ;}
Set{M_name =Value;}
}
Public Accessable_test This[IntI]
{
Get{ReturnAccesses [I];}
Set{Accesses [I] =Value;}
}
Public Accessable_test This[StringSTR]
{
Get{Return Null;}
Set{}
}
// Error: A member with the same parameter type has been defined.
// Public String This [int I]
//{
//Get {return NULL ;}
//Set {}
//}
}
ArticleReference: http://www.cnblogs.com/wjfluisfigo/archive/2009/05/04/1448079.html