C# Indexers and Properties
Terms
undefined, object
copy deck
- What is the formal declaration of an indexer?
- [attributes] [access modifiers] indexer-declarator {accessor-declarations}
- The indexer-declarator is a rather complex statement. Show its formal representation.
-
type this [formal-index-parameter-list]
OR
type interface-type.this [formal-index-parameter-list] - What are the allowed modifiers for an indexer?
-
private
public
internal
protected
new - What are accessors?
- use get and set as with properties.
- How are property values set and retrieved.
- Throught 'Accessors'. The get and set part of the property declaration.
- What is the formal representation of a property?
-
[attributes] [modifiers] type identifier {accessor-declaration}
OR
[attributes] [modifiers] type interface-type, identifier {accessor-declaration} - Are properties variables?
- No. Properties cannot be treated as variables. They cannot be passed as ref or out parameters.
- How is a static property manifested?
- A static property can only be accessed through a type name.
- Can 'this' be used to access a static property?
- No. Attempting to access a static property use 'this' is an error.
- Using 'name' as the private member of a class, create a get accessor for name. 'name' is type string.
-
...
private string name;
public string Name
{
get
{
return name;
}
}
... - Using 'name' as the private member of a class, create a set accessor for name. 'name' is type string.
-
...
private string name;
public string Name
{
set
{
name = value;
}
}
... - How is a read-only property created?
- Declare only the 'get' accessor.
- How do you create a write-only property? Why would you want to?
-
Create only the 'set' accessor.
To build an internal use counter, for example. - Why not modify the value of a property in the 'set' accessor?
- This is considered bad practice as it will probably result in strang and mysterious bugs. Not to mention it my be hard to understand.
- Create an interface property called 'ITarsalFormula' having public property 'int tFormula'.
-
public interface ITarsalFormula
{
//tarsal formula interface
int tFormula
{
get;
set;
}
} - If a class is inheriting from two interfaces and both have the property 'Length' (for example), how must you reference the 'Length' property in your code?
- You must use the fully qualified reference.
- Is the order of the 'get' and 'set' accessors within the property declaration important?
- No. They may appear in either order.