I have a class that has the variable "Magic". This is a 4 char string. Can I do something like this in C#?
string offset = chunkList["_blf"].offset;
*Assume that "chunkList" is an IList/List of "chunk" objects.
The square brackets are used as an attribute selector, to select all elements that have a certain attribute value. In other words, they detect attribute presence.
The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value. For example, in the string shown below, the 14 characters are indexed left to right from postion 0 to position 13.
You can see that HTML contains pieces enclosed in angle brackets (' < ' and ' > '), which are also known as the less-than and greater-than symbols. Each of these angle-bracket-enclosed pieces, called a tag, does not appear to the user, but rather gives information about the page's structure.
You could use something like this:
string offset = chunkList.Find(x => x.Magic == "_blf").offset;
Better is to check if Find retuns null:
Chunk chunk = chunkList.Find(x => x.Magic == "_blf");
if (chunk != null)
offset = chunk.offset;
Yes, you can create an indexer on your class:
public string this[string s]
{
get
{
// retrieve value
}
set
{
// set value
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With