Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# marking member as "do not use"

Tags:

public class Demo {     private List<string> _items;     private List<string> Items     {         get         {             if (_items == null)                 _items = ExpensiveOperation();              return _items;         }     } } 

Other methods in the Demo class will have access to the _items field. Since I'm using a property to lazy load the items, I do not want another developer to mistakenly try to use the _items field.

I know there is the ObsoleteAttribute that I may use, but this field isn't technically obsolete.

Is there a better way to mark a member as "do not use"?

like image 458
nivlam Avatar asked Jan 22 '12 21:01

nivlam


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

Though it's not a general technique for what you want to do (and there isn't one and, as the other answers cover, you need to trust other devs), in this instance, you could create a Lazy<List<T>> (assuming .NET 4 or later - though it's easy to backport)

class Demo {     readonly Lazy<List<string>> _items;     public Demo() {         var _items = new Lazy<List<string>>( ExpensiveOperation);     }     List<string> Items { get { return _items.Value; }}  } 

The readonly / non-mutable approach is generally the way to go for backing fields either way.

EDIT: Based on @Timwi's answer (go +1 if you like the idea) one can go to town on it, and in a JavaScript stylee use capability-based restriction to not even expose the Lazy field, just an operation closed over it (Also incorporates @Mr Disappointment's ReadOnlyCollection suggestion):

class Demo {     readonly Func<ReadOnlyCollection<string>> _getItems;     public Demo() {         var items = new Lazy<List<string>>( ExpensiveOperation);         _getItems = () => items.Value.AsReadOnly();     }     ReadOnlyCollection<string> Items { get { return _getItems(); }}  } 

And thus endeth our stupid coding tricks post.

like image 178
Ruben Bartelink Avatar answered Sep 24 '22 16:09

Ruben Bartelink