This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Should I document my private methods?
Is it good practice write comment for private fields and private methods? I've write a simple library and I don't really know if add comment for my private fields or not.
If you have something relevant to say, it is a great idea to document it even for private members, as a guideline for future maintenance of the class. But boilerplate comments like "Gets or sets the Foo property" is pure noise for private code IMO.
Related
This question already has answers here:
What is the difference between getter-only auto properties and expression body properties?
(2 answers)
Closed 7 years ago.
I have a class that uses some services. I need each service to be instantiated on creation of the class. In C# 6 I can see 2 ways of doing this but I'm not sure which would be correct...
protected static SomeServiceType Service => new SomeServiceType();
alternatively I could use an autoproperty initialiser...
protected static SomeServiceType Service { get;} = new SomeServiceType();
What are the advantages/drawbacks with each approach?
Many thanks
I believe
the former ("Expression-bodied members") calls new SomeServiceType() every time the property is read
the latter ("Auto-property initializers") calls it once on instantiation, and returns the created instance every time the property is read.
It sounds like you want the latter.
This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx
This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
Newbie Incoming, coming with a question about Unity, and C# in particular. This could be really simple, but I could not figure it out.
What's the difference between public and private mean? I don't get it. public, private, it boggles my mind. I just can't get it through my head, which doesn't happen very often. Can someone explain it to me like I'm five? It would really help me out on my journey of making a ball move across the ground. Thank you in advance.
Good night .
The difference is :
Private : Those variables or " functions" which may only be used in the source class .
Public : Those variables or "functions" that can be used in various class .
Classes: A class is a construct that allows create your own custom types by grouping variables of other types , methods and events. A class is like a blueprint . Defines the data and behavior of a type.
This question already has answers here:
What does the tilde before a function name mean in C#?
(6 answers)
Closed 9 years ago.
In doing a code review I came across this:
public class ClassTilda {
~ClassTilda(){
//code
}
}
Why is this building and what is that tilda? Can you provide a reference?
This is Destructor, which is basically not suggested to use in C#.
Destructors are used to destruct instances of classes.
Like was mantioned in comments: there are still cases when you would like to manage, but in most cases it's avoidable as you have:
IDisposable when dispose is called on instance of your object interface
Finalize() when GC is going to clean your type, so called by GC itself.
This question already has answers here:
labeling a group of members as private/public in c#
(3 answers)
Closed 9 years ago.
Is there a way in C# to declare a group of variables and methods as private or public like in C++ (example below). I am just trying to avoid typing a million "public"s and "private"s.
class Foo
{
private:
int Alpha;
string Dog;
public:
bool Bites;
bool Bad;
}
I keep getting an error in C# and have exhausted my internet search abilities. Thanks
No. You need to specify visibility for each member.
private is default for members, so it is safe to omit it (unless your coding guidelines tell you must specify). More details/links - Default visibility for C# classes and members (fields, methods, etc)?