Private Static C

Private Static C



⚑ ALL INFORMATION CLICK HERE πŸ‘ˆπŸ»πŸ‘ˆπŸ»πŸ‘ˆπŸ»

































Private Static C


Sign up with email
Sign up




Sign up with Google



Sign up with GitHub



Sign up with Facebook




c# static-methods private-members


2,536 2 2 gold badges 20 20 silver badges 33 33 bronze badges



Well it makes it clear that it isn't related to any particular instance of MyClass . Generally speaking, if a method is logically not related to a specific instance, making it static makes that clearer. It also means that you can call it from other static methods without having to obtain a reference to an instance.

–  Jon Skeet
Jan 6 '18 at 12:48






"I tested and found out that, when I use the static keyword for MyPrivateMethod, I can not call any other methods of the class that are NOT private static." Then your testing was incomplete. private static methods can access any other static methods in the class, whether they are private or not.

–  JLRishe
Jan 6 '18 at 12:51



You told, 'making it static makes that clearer.' Do you mean it's just useful for code review and has not any influence on performance? And Also you told ' you can call it from other static methods without having to obtain a reference to an instance'. When the method is private I have no access from other class

–  Elnaz
Jan 6 '18 at 12:55



ReSharper sees that you are using MyPrivateMethod to share code among other methods of your class, notices that the method does not currently access any non-static methods or fields, and suggests adding static to its declaration. It tries to guess your intention to make a "helper method," rather than driving your decision.

–  Sergey Kalinichenko
Jan 6 '18 at 12:57



@Elnaz I don't really understand what you just said, but yes, private static methods can call any other static method in the class. See here: ideone.com/ijLhEr

–  JLRishe
Jan 6 '18 at 13:07


36.8k 20 20 gold badges 113 113 silver badges 160 160 bronze badges



Thanks. Do you have any link as reference to help me understanding it more? Particularly, about your first paragraph.

–  Elnaz
Jan 6 '18 at 13:08






I'm pretty sure the reason Resharper recommends it has nothing to do with the CPU savings from not having this on the stack.

–  JLRishe
Jan 6 '18 at 13:09






@JLRishe you mean to say there is no benefit on CPU saving using static method?

–  Akash Kava
Jan 6 '18 at 13:12



@AkashKava No, I mean to say I don't think that the situation you described is the reason Resharper suggests it, at least not the only reason (you claimed that was Resharper's reason in the last paragraph). Do you have some citation that explains their reasons for recommending it?

–  JLRishe
Jan 6 '18 at 13:17






@JLRishe msdn.microsoft.com/en-us/library/ms245046.aspx check rule description, however this is MSDN but I am sure, Resharper guys are following this as rule set.

–  Akash Kava
Jan 6 '18 at 13:26

JPMorgan Chase Bank, N.A. Moscow, Russia
Senior React.js / Relay.js / TS Engineer
Senior Backend Engineer in Full Remote Environment
Solutions Architect - Discovery - Remote

Stack Overflow

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness


Products

Teams
Talent
Advertising
Enterprise



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Join Stack Overflow to learn, share knowledge, and build your career.
I have a public class called MyClass.cs . It has 3 methods:
I use ReSharper as a refactoring tool. It suggests using static for MyPrivateMethod .
But what is the usage of this keyword, here? Since the method is private and will not be used in other class that want to use an instance of MyClass.
I tested and found out that, when I use the static keyword for MyPrivateMethod , I can not call any other methods of the class that are NOT private static. But yet I don't know what is the usage? Is there any benefit on storage or time optimization, for example?
Members that do not access instance data or call instance methods can
be marked as static (Shared in Visual Basic). After you mark the
methods as static, the compiler will emit nonvirtual call sites to
these members. Emitting nonvirtual call sites will prevent a check at
runtime for each call that makes sure that the current object pointer
is non-null. This can achieve a measurable performance gain for
performance-sensitive code. In some cases, the failure to access the
current object instance represents a correctness issue.
One more benefit is calling sequence, when you call an instance method, code generated will push instance of this onto stack as first parameter and rest of the parameters for the method will be pushed onto the stack. So every instance method call requires one more additional stack push for this in along with other method parameters.
If you convert your method to static, static method calls do not require this so one less push operation for CPU. It doesn't seem big benefit for single call.
But if your method will be used very frequently and if you have couple of methods that do not require this then it can save significant CPU time, especially in graphics and scientific calculations.
This is the reason Resharper is suggesting you to change method to static when method does not reference anything that is part of this .
This is il generated for calling an instance method in "InstanceAdd"
and this is the il generated for instance method in "StaticAdd"
If you look at "StaticAdd", there is no ldarg.0 , which is this . For every method call, there will always be ldarg.0 as first instruction and then rest of the parameters will follow.

By clicking β€œPost Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo Β© 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . revΒ 2021.2.2.38474


c # - Advantages to Using Private Static Methods - Stack Overflow
What is the usage of private static method in c #? - Stack Overflow
How to initialize private static members in C ++?
Using private static methods Instead of private methods In C #
private static int C # Code Example
Β© Copyright 2021. All Rights Reserved.
Here we will see how to initialize the private static member variables initialization in C++. We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class.
To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.
The following code will illustrate the of static member initializing technique.

Outdoor Cpe
Overwatch Masturbate
Private 2002
Double Penetration Porno Gif
Bbw Outdoor Porno

Report Page