Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8252173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:29:06+00:00 2026-06-08T00:29:06+00:00

Slightly possible duplicate : Instance method vs. static method with ref parameter If I

  • 0

Slightly possible duplicate :

Instance method vs. static method with ref parameter

If I have a class Employee, and there’s a method AddEmployee which adds the employee to database. There are tho methods that I can adopt, one is like this,

protected void AddEmployee(SQLConnection con)
{
    // this is an instance method, 
    // connection is passed in parameter
    // add this class to database, using the connection
}

and I would call it like this

   var emp = new Employee();
   // set its properties
   emp.AddEmployee(theSQLConnectionObject);

The other approach is that, I create a static method, and then pass an instance of Employee class, and a SQLConnecion, and then add that instance of employee class to the database, like this

static protected void AddEmployee(Employee emp, SQLConnection Con)
{
    // this is static method
    // connection again in parameter
    // add emp class to database, using the connection
}

this can be added as,

var emp = new Employee();
// set its properties
Employee.AddEmployee(emp, theSQLConnectionObject);

I wanna know which one is good approach, which one do you prefer, and why? Also, I wanna know C# specific, the related question isn’t any lang specific.

Now, in starting I said slightly possible duplicate because of this part.
I was reading CLR Via C#, and in Chapter 8 Section Type Constructors, it goes like

When compiling a method, JIT compiler determines weather it must emit
a call to execute a type constructor into the method. If the JIT
compiler decides to emit the call, it must decide where it should emit
the call. There are two possibilites

  • Precise semantics, emit the call immediately before code that would create first instance or immediately before code that access a
    noninherited filed of member of the class.

  • Before-field-init semantics, emit the code sometime before the code fist access a static field or a static or instance method, or invokes
    an instance constructor.

Some more description, and then an example of performance comparison was given, and there was a quite a lot of difference in performance, I am not including that for sake of brevity, but if anyone want that, comment and I’ll update the question.
After the example, he continues

When C# compiler sees a class with
static fields that use inline initialization, compiler emits
before-field-init in metadata’s type definition table. When it sees a
class with explicit constructor, it doesn’t emits before-field-into
into metadata.

Now, if I have a static field in my Employee class, how would the situation differ? From what I can think, there would be 4 differnt cases

  • AddEmployee is instance, class has no static constructor

  • AddEmployee is instance, class has a static constructor

  • AddEmployee is static, class has no static constructor

  • AddEmployee is static, class has a static constructor

How would the performance differ in all these cases, assuming that method AddEmployee is called at a buttonClick and each time, an instance of employee is created (in both cases, static method or not, because they both require an instance), thus each time a new instance is created. Also, would it matter if this AddEmployee was called somewhat differently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:29:09+00:00Added an answer on June 8, 2026 at 12:29 am

    I suggest you not to use a static method in this situation. Static methods are very ugly solution in many cases. The better way will be if you create a data mapper class with instance method, it can help you in future when you decide that you need to use inheritance to extend the behavior of you data mapper class to get different saving action, for example. Don’t think about perfomance at first, especially in this moments, the experience of good programmers shows us that we need to solve the problems with perfomance when you face with it. You won’t finish your application at all if you optimize each line of code of your application. In common ways, sometimes there are 2-3 places where we need to improve perfomance of our code, but sometime there are no places of this kind at all. So, try to think about your design at first, about it readability and extensibility.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I slightly remember that there is an class which is capable of stretching an
A slightly archaic question I'm afraid but here goes: I have a program which
I have a couple slightly modified / non-traditional setups for feedforward neural networks which
I have this page in which I slightly modified the jQuery autocomplete to make
Possible Duplicate: How to rewrite URL without refresh, like GitHub.com Hi all, I have
Possible Duplicate: Regex/Javascript to transform Degrees Decimal Minutes to Decimal Degrees I have some
Possible Duplicate: Instantiate a class with or without parentheses? This is a minor quibble,
Possible Duplicate: Combine Gyroscope and Accelerometer Data I have read a number of papers
I'm slightly worried that this may be a duplicate, but I have searched the
Possible Duplicate: C++: When to use References vs. Pointers Could operator overloading have worked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.