In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand the C# indexer". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. Summary
Indexers make it easy for you to create classes, structures, or interfaces syntactically so that client applications can access them as if they were arrays. The compiler will generate an Item attribute (or, if IndexerNameAttribute exists, a named property) and the appropriate accessor method. Indexers are often implemented in types whose primary goal is to encapsulate internal collections or arrays. For example, suppose you have a class TempRecord that represents the temperature (in degrees Fahrenheit) recorded at 10 different time points over a 24-hour period. This class contains an array temps of type float [], which is used to store temperature values. By implementing an indexer in this class, the client can access the temperature in the TempRecord instance in the form of float temp = tempRecord [4] rather than float temp = tempRecord.temps [4]. Indexer notation not only simplifies the syntax of client applications, but also makes classes and their goals more easily and intuitively understood by other developers.
Syntax statement:
Public int this [int param] {get {return array [param];} set {array [param] = value;}} II. Application scenarios
Here we share the design encapsulation point of view using indexers. The scenario is to encapsulate a helper class of redis. Before that, let's take a look at a simple official example.
Using System;class SampleCollection {/ / Declare an array to store the data elements. Private T [] arr = new T [100]; / / Define the indexer to allow client code to use [] notation. Public T this [int I] {get {return arr [I];} set {arr [I] = value;} class Program {static void Main () {var stringCollection = new SampleCollection (); stringCollection [0] = "Hello, World"; Console.WriteLine (stringCollection [0]);}} / / The example displays the following output:// Hello, World.
The encapsulation (pseudocode) of the RedisHelper class, which has the advantage of not having to go to a lot of trouble to set the db number of redis.
Public class RedisHelper {private static readonly object _ lockObj = new object (); private static RedisHelper _ instance; private int dbNum Private RedisHelper () {} public static RedisHelper Instance {get {if (_ instance = = null) {lock (_ lockObj) {if (_ instance = = null) {_ instance = new RedisHelper () } return _ instance;}} public RedisHelper this [int dbid] {get {dbNum = dbid; return this } public void StringSet (string content) {Console.WriteLine ($"StringSet to redis db {dbNum}, input {content}.");}}
Call:
RedisHelper.Instance [123] .StringSet ("Test data")
Running effect:
This is the end of "how to understand the C# indexer". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.