0

I have web app written in c#. It's consisted of three projects. Two of them are asp.net mvc projects and one of them is a simple project with only classes which I use in the two mvc projects. So, the two mvc apps reference the third project.

I have a static method in the non-mvc project which basically does this: it has two input parameters: string text, MyEntities db. MyEntities is an instance of my database, the .edmx file.

Inside the method, I parse the text, divide it into separate lines, get a part of them. For that part, depending on the user, I check if there is a record in db.tblToy with that name.

For every user there is a different db. So, I take the user_id and according to that, I search in the according db, in the tblToy table. By dangerous I mean, if the method returns incorrect results. For example, for user1 it will check in the db for user2, because it's static.

That method is static. I don't call it with a class instance. My question is this: is it of any danger that this method is static?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

2 Answers2

1

No, the fact that it's static isn't any more dangerous than it being an instance method. What you put in that method is what matters.

The only difference between static and instance methods is that you need to call one on an instance of a class, and the other is just called on the type itself.

This answer, from the master, might help:

Static methods aren't inherently thread-safe. They're treated no differently by the CLR than instance methods.

Community
  • 1
  • 1
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • I think the dangers of public static methods really only present when the method either modifies and/or depends on a static property on that class -- a rule that doesn't really change between static and instance -- but it *can* be potentially more dangerous not because of what it is but rather because of the way developers tend to use static methods, which in reality comes down to laziness (not wanting to have to new up a new object). I'm basically agreeing, but qualifying it further. – Sinaesthetic Nov 10 '15 at 19:29
1

No, static methods are free from danger in this scenario, if it process information only from parameters. The game changes when the method work on static variables/auto-implemented properties

T-moty
  • 2,679
  • 1
  • 26
  • 31