Using private static methods Instead of private methods In C# [duplicate] - c#

This question already has answers here:
What is better? Static methods OR Instance methods
(7 answers)
Closed 9 years ago.
When its recommended to use a private static method instead of a private [instance] method?
EDIT:
i am looking for a good (or best practice) about that .i am wondering Is this technic used by microsoft or not?does anybody know something about that ? i coudnt find any blog ,article or sample source code that explains this topic.
any help would be highly appreciated.

Static word in the beginning of the method declaration, basically is a sign of stateless, so what is happening inside is a pure action, or at least should be.
If you want to use private static: use it like API functions of your class that just make some calculations/reports... and not change the actual state of the object, which basically is done by instance methods.
This is an expected way of implementing, which doens't mean that is mandatory, but as it's expected, it will help other developers understand your code, and help understand code to you after a couple of years, when you will come back to your project and already have forgot everything.
Regards.

Related

Purpose of static classes [duplicate]

This question already has answers here:
In C#, what is the purpose of marking a class static?
(5 answers)
Closed 4 years ago.
While learning about static classes, I went through many forums as per my understanding I got to know that static class members can be accessed directly using class name and hence not required to create object of that class which in turn helps save memory and faster execution of program.
So my question is if this is the case then why not always use static class over normal class.
I know my question may be little weird, also I may be wrong with the concepts. Please if any one can explain this in detail with example. would be great help.
A static class can not be instantiated. E.g. you can't use new to create an object of that class in C#.
There are many situations where you might want to create multiple objects of a class.

what are ["foo bar"] lines in c#? [duplicate]

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 6 years ago.
So I'm coming over from Java, and I've been seeing (and using), a ton of these:
["foo bar"]
void method(param params)
{
..code things
}
Can someone explain to me what they are? I don't even know what they're called, so I can't manage to figure it out from documentation.
They are called .NET Attributes. You can read about it for example here: What are attributes in .NET?
They are called Attributes.
There are many uses, such as:
Declare tests methods
Add description on enum values
Dependency Injection with MEF
You can do things with reflection
and so on.
They are attributes. You can use them to decorate your code.
The libraries providing the attributes usually use reflection to get this decorated (meta) information from your code at runtime.
For example, the XmlSerializer uses them a lot.

Use of extension methods [duplicate]

This question already has answers here:
When do you use extension methods, ext. methods vs. inheritance?
(7 answers)
Closed 9 years ago.
I have been using C# extension methods for a while now and I find them to be really handy. However I am not really sure of what is the most ideal situation to use them, I feel at times I abuse them. When would you recommend the use of extension methods ?
IMO when either it's really an extension (and not a core/critical operation), or when it's a shortcut.
An example of an extension:
Often in sandbox applications (but it could also be used in real ones, of course) is extending IEnumerable with a Print method.
The print method shouldn't be there (that is: it shouldn't be a part of the IEnumerable class), but it's helping and making the syntax easier and cleaner. Also, you probably wouldn't want to ship a library with it as a part of that class.
An example of a shortcut:
Another thing I find myself often creating is an helper extension for objects with containers. Instead of calling Items.Add and similar, I just make an AddItem extension-method.
Something to consider is that it's just syntactic sugar, that is, it's for the you - the developer. So for .NET types and such use it when you think it's a good idea and will make things cleaner.
When it comes to "Should this method be an extension or a member?" see the first sentence in this answer, and also look here & here for more information.

C# Reflection: Is it possible to view the code of a constructor gained using reflection? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# Can I use reflection to inspect the code in a method?
So I am messing around with reflection and something struck me. Is it possible to view the code of a constructor obtained using the GetConstructor() method without navigating to the file and viewing it in visual studio. Is it possible for example to get the ConstructorInfo object and then print out the code that is associated with that constructor to the console window? Might seem a silly question but I have never really needed to look into reflection before so I am pretty new to it and am not sure as to how much it is capable of.
Also as a side note what is it that determins which constructor will be used as the default? Is it just the one with the least parameters?
Basically, code is only available in form of MSIL instructions. See Can I use reflection to inspect the code in a method? for more info.

Static class vs instanced class [duplicate]

This question already has answers here:
Closed 13 years ago.
Duplicate
Should C# methods that can be static be static?
Please forgive me if this question seems elementary - I'm looking over some source code that otherwise looks pretty good, but it's raised some questions...
If a given class has no member data - i.e. it doesn't maintain any sort of state, are there any benefits in not marking that class as a static class with static methods?
Are there any benefits in not marking methods which don't maintain state as static?
Thanks!
EDIT: Since someone brought it up, the code I'm looking at is written in c#.
Yes. I can think of some reasons:
Ease of mocking and unit testing
Ease of adding state
You could pass it around (as an interface or something)
I would say there is a benefit to making them static methods of the class, and on top of that making the class abstract. That way, it's clear to the programmer that this class was never intended to be instantiated and the methods are still available.
There might be a benefit in leaving methods that don't change state non-static, if you intend (or think you might intend) to inherit from the class and override those methods with code that does change state. Though in that case it really should be an abstract class.
If a class doesn't maintain any state, doesn't have any instance methods, and could be implemented entirely with class methods, I usually take a long hard look at whether its behaviors would in fact make more sense somewhere else. I find they usually seem to have a lot of methods along the lines of doSomethingWithItem(Item item), which would make more sense as an instance method in Item.
Yeah, I know this doesn't answer your question, but I think others have covered that pretty well already, and I wanted to get another perspective out there.

Categories

Resources