Repository navigation
CSharp
The rules of the method stereotypes, the free-function stereotypes, and the class stereotypes hold in C# as in every language of Stereocode. The section Data Members and Calls of the method stereotypes states the conventions by which Stereocode determines the facts of these rules in every language. This page gives the forms that these facts take in C#, the built-in lists that Stereocode reads for C#, and one labeled example of each stereotype.
- A function among the top-level statements of a program is a free function. Structs and interfaces get their stereotypes with the options
-tand-i. - The accessors
getandsetof a property are methods of the class. A set accessor returnsvoidand receives the new value of its property as the hidden parametervalue. The type of this parameter counts for collaborator, as the type of any other parameter does.
- A method reaches its object through
this. - An event without accessors is a data member of its delegate type, while a static event counts as a static field. A call to any method on an event, such as
Closed.SafeInvoke(this, args), reads the event and counts as a call of a free function.
- A parameter shares its object with the caller when it is a
reforoutparameter or a parameter of a class type. The modifierinbefore a parameter of a class type leaves the object shared, sinceinmakes only the reference read-only. A struct or an enum of the archive passed by value is a copy that shares nothing, also with generic arguments such asSlot<string>. - Using directives decide which class of the archive a type name refers to. A directive for every type of a namespace, such as
using Model;, yields to the types that the other files of the namespace of the current file declare. Stereocode follows a using alias directive, such asusing Alias = N.Item;, for the type of a data member. - Stereocode reads a type without its attributes. The arguments of an attribute inside a method body, such as
[Obsolete("x")]on a local variable, count as no read and no call. - Stereocode reads
System.Nullable<int>asint?, a primitive type that makes no method a collaborator. - The variable of a type pattern, such as
foundofother is Gauge found, is a local variable of the type that the pattern names. The variables of a tuple declaration, such asvar (low, high) = pair;, of a using declaration, and of a query expression, such asitemoffrom item in items, are local variables as well. - A deconstruction such as
(low, high) = pair;writeslowandhigh. The discard_, as in_ = Run();orParse(text, out _), is no variable. - The null-forgiving operator
!changes no fact about the variable that it follows.buffer![0] = valuewritesbuffer,writer!.Flush()is a call onwriter, andreturn name!;returnsname. - A name in the argument of
nameof, such aslabelofnameof(label), reads no value. - The name before
=or:in an object initializer, awithexpression, or a property pattern, such asLeftofother with { Left = 1 }, names a member of the created or matched object and reads no data member.
- A method with the specifier
asyncreturns the value of its task.TaskandValueTaskcount asvoid, whileTask<T>andValueTask<T>count asT. A method withoutasyncreturns the task itself. -
return ref head;returns the data memberhead, asreturn head;does.
- A call without an object whose name matches no method of the type or of its ancestors in the archive is a call to the object itself in two cases. First, the name is an accessor of
System.Objectwith its number of arguments, namelyEquals,GetHashCode,ToString,GetType, orMemberwiseClone. Second, the type has a parent outside the archive. Such a call stays a call of a free function where its name is a static method that the type can access, such as a static method of an ancestor or of ausing staticdirective. - A call on a data member through an extension method of the archive, such as
basket.IsEmpty()forIsEmpty(this Basket basket), counts as the static callBasketExtensions.IsEmpty(basket)does. It reads the data member and counts as a call of a free function. - A variable passed with
reforout, such ascountinInterlocked.Increment(ref count), counts as written by the call, whether the variable is a data member, a parameter, or a static field, since the called method may write the variable. - A target-typed
new(), a creation with an initializer such asnew Report { }, and an anonymous objectnew { Total = 1 }are constructor calls.
Stereocode reads four built-in lists for C#. The list files of the options -p, -c, -s, and -q add entries to these lists under the section [C#], as the README states. Each list below opens with a click.
Primitive types (37 entries)
A type of this list makes no method a collaborator and no return type of a factory.
bool, Boolean, Byte, byte, Char, char, Decimal, decimal, Double, double, float, Half, int, Int128, Int16, Int32, Int64, IntPtr, long, nint, nuint, SByte, sbyte, short, Single, String, string, uint, UInt128, UInt16, UInt32, UInt64, UIntPtr, ulong, ushort, Void, void
Ignored calls (8 entries)
The rules ignore a call of these names, also after a qualifier, as System.Console.WriteLine for Console.WriteLine.
Console.Error.WriteLine, Console.Write, Console.WriteLine, Debug.Assert, Debug.WriteLine, Environment.Exit, nameof, Trace.WriteLine
Specifiers and containers (62 entries)
Stereocode strips these entries from a written type before it reads the names of the type. A container counts by the types of its elements. Each entry is a regular expression, such as \.\.\. for ....
<, >, \*, \?, abstract, async, BitArray, BlockingCollection, ConcurrentBag, ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, const, Dictionary, event, explicit, extern, HashSet, HybridDictionary, ICollection(?=<), IEnumerable(?=<), IList(?=<), implicit, in, internal, IReadOnlyCollection, IReadOnlyList, ISet, KeyedCollection, LinkedList, List, NameValueCollection, new, Nullable(?=<), OrderedDictionary, out, override, params, partial, private, protected, public, Queue, readonly, ReadOnlyCollection, ReadOnlyDictionary, ref, required, scoped, sealed, SortedList, SortedSet, Stack, static, StringCollection, StringDictionary, this, Tuple, unsafe, ValueTuple, virtual, volatile
Query methods (9 entries)
A call of these methods only reads the variable that the call is on. An entry with a slash, such as GetValue/1, holds only for that number of arguments.
Contains, ContainsKey, Count, Equals, GetHashCode, GetType, GetValue/1, ToString, TryGetValue
Each example below is the declaration of an element from the stereotype cases of Stereocode, whose expected stereotypes are written by hand in @stereotype comments. Each link opens the case on the branch develop. The second column gives every stereotype of the element. A stereotype without an example has no case in C#.
TABLE 1. Examples of the method stereotypes in C#.
| Stereotype | Stereotypes of the element | Declaration |
|---|---|---|
| main | main |
public static void Main(string[] args) { Report(args.Length); } |
| predicate | predicate |
public static bool IsEven(int value) { return value % 2 == 0; } |
| property | property |
static int Thrice(int x) { return x * 3; } |
| global-command | global-command |
public static void Hit(int n) { hits += n; } |
| command | command |
public static void Touch(Node p) { p?.y?.Z(); } |
| factory | property factory |
public static Widget MakeWidget(int style) { Widget widget = new Widget(style... |
| literal | literal |
public static int Answer() { return 42; } |
| wrapper | wrapper |
public static void Run(Action action) { action(); } |
| empty | empty |
public static void Noop(int value) { } |
| unclassified | unclassified |
static void ResetPlain(Holder h) { h = null; } |
TABLE 2. Examples of the free-function stereotypes in C#, which the option -f assigns.
| Stereotype | Stereotypes of the element | Declaration |
|---|---|---|
| boundary | boundary |
public class Exporter |
| commander | commander small-class |
public abstract partial class Job |
| controller | controller |
public class Spooler |
| data-class | data-class |
public class Account |
| data-interface | data-provider data-interface small-interface |
public interface IChain |
| data-provider | data-provider |
public class Circle |
| data-struct | data-provider data-struct small-struct |
private struct Pair |
| degenerate | entity degenerate |
public class Plugin |
| empty | empty |
public interface IShape |
| entity | entity boundary |
public class Measure |
| factory | factory controller |
public class WidgetFactory |
| large-class | large-class |
public class Box |
| large-interface | large-interface |
public interface IShape |
| large-struct | large-struct |
public struct Frame |
| lazy-class | lazy-class |
public class Settings |
| lazy-interface | data-provider lazy-interface |
public interface ILazy |
| lazy-struct | lazy-struct |
public struct Slot |
| minimal-entity | minimal-entity commander |
public class Counter |
| pure-controller | pure-controller |
public class Dispatcher |
| small-class | small-class |
class Clock |
| small-interface | data-provider data-interface small-interface |
public interface IChain |
| small-struct | data-provider data-struct small-struct |
private struct Pair |
| unclassified | unclassified |
public class Large22 |
TABLE 3. Examples of the class stereotypes in C#, where large-class, lazy-class, data-class, and small-class of a type of another kind take that kind in place of class, such as data-struct.