2

What are the naming conventions for internal members in C#?

For example, default access modifier for WPF controls is internal - how should I name them?

Peter Sivák
  • 557
  • 7
  • 10

3 Answers3

3

All non-public members should be lowerCamelCase.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

you don't usually name WPF controls. Unless you intend to use things such as UIA on them.

That being said, take a look at the Official C# Naming Conventions

Edit: Even if you plan to use UIA on your application, you should avoid naming controls unless needed. This encourages good practices (MVVM, separation of UI and logic) and prevents lazy / unexperienced developers from resorting to winforms-like code behind practices.

UIA may also rely on properties such as AutomationProperties.AutomationId, therefore completely removing the need to give a name to WPF controls, unless of course you will use them as storyboard targets or you need some ElementName binding.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • What is UIA? I googled that it means UI Automation, but I have heard about it for the first time. – Peter Sivák Jun 12 '13 at 22:04
  • I name a WPF control, when I want to do something with it programmatically in runtime - for example disable some controls when something happens, or add text to `TextBox`, etc. Is it bad to do it? – Peter Sivák Jun 12 '13 at 22:10
  • @PeterSivák yes it's bad to to that. if you need to programatically disable a control, then you **bind** it's `IsEnabled` property to some bool property in the ViewModel and manipulate that. – Federico Berasategui Jun 12 '13 at 22:18
  • I have read [General Naming Conventions](http://msdn.microsoft.com/en-us/library/ms229045.aspx) you told me, but there was mentioned nothing about `internal` members :) . By the way, why there is an option to name WPF control, when it is bad? – Peter Sivák Jun 12 '13 at 22:19
  • @PeterSivák as advised by @SLaks, you should use `lowerCamelCase`. Still, I insist that you should not name WPF controls. – Federico Berasategui Jun 12 '13 at 22:21
  • @PeterSivák There's also an option to use `goto` in C#. There's also an option not to create any classes and manipulate all your data as a `Dictionary` within an application. The fact that something is possible doesn't mean it's good. Manipulating UI elements in procedural code is not needed nor desired in WPF. Read up on MVVM. – Federico Berasategui Jun 12 '13 at 22:35
  • 1
    Just to clarify, you can set a UI Automation Name without setting a WPF Name. You can set the former with the `AutomationProperties.Name` attached property and the latter with the `FrameworkElement.Name` dependency property. For some controls, the UIA Name defaults to using the `FrameworkElement.Name`, if it is specified. – Mike Zboray Jun 12 '13 at 22:39
1

Sometimes non-public members can be _lowerCamelCase.

Here is a link Naming convention - underscore in C++ and C# variables

Community
  • 1
  • 1
Vadym
  • 1,067
  • 1
  • 13
  • 24