question

PierreStempin-3028 avatar image
0 Votes"
PierreStempin-3028 asked TimonYang-MSFT commented

Which convention to follow between C# and DotNet ?

Hello, I just noticed that some elements of the DotNet and the C# naming convention contradict each other, here for the static fields:

For DotNet (https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members)
It says: 'For example, do not use "g_" or "s_" to indicate static fields.'

But for C# (https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)
It says strictly the inverted thing: 'When working with static fields that are private or internal, use the s_ prefix and for thread static use t_.'

So, I'm using Unity which use both DotNet and C#. Which naming convention should I follow?
How am I supposed to write my static fields? With a 's_' prefix or not?

Thanks.

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@PierreStempin-3028
The naming convention has nothing to do with the language, it is just a general convention between programmers.
As long as it follows consistent, logical, and predictable requirements, it is a good naming convention.
As far as these two options are concerned, I personally choose to use the prefix.

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Ask ten developers their recommendation you will get at least 8 to ten opinions.

My recommendation, using the following is to prepend a private variable with an underscore with the first letter lowercase. Static public and private methods camel case e.g. ReadProductsTask.

Don’t care for s_ prefix or t_.

In the end if this is for personal use, go with suites you best and when looking at a field for instance know by looking at the field know it’s scope. If working in a team or sharing say a GitHub repository there needs to be a unanimous agreement on how fields, properties, methods and delegates/events are named.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.