Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when you call a static method in C#?

Tags:

c#

static

When you create an instance of a class, all the variables in that instance is specific to that instance and get killed when the instance is out of scope. But how does it work in a static method? Suppose two people call System.Math.Abs() at exactly the same time. How does the runtime differentiate between the two callers? Is this where threading comes in? Are separate threads automatically created for each caller?

like image 984
developer747 Avatar asked Dec 22 '11 15:12

developer747


People also ask

What happens when a static method is called?

If you try to call a static method via an instance you will get warning as you should avoid it, so you wont get a valid case but yes its logical to allow static call through instance.

What does a static function do in C?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

Does a static method return anything?

it is a static method (static), it does not return any result (return type is void), and. it has a parameter of type array of strings (see Unit 7).

Can I call a static function from another file in C?

Functions in C are global by default. To make them local to the file they are created, we use the keyword static before the function. Static functions can't be called from any other file except the file in which it is created. Using static function, we can reuse the same function name in different files.


1 Answers

When you create an instance of a class, all the variables specific to that instance are killed when the instance is out of scope.

The variables -- usually called "fields" are deallocated after the lifetime of the instance. Scope is the region of program text in which the compiler recognizes something by its name; liftime is the portion of time during which a storage location is valid. Scope and lifetime are often confused.

But how does it work in a static method?

Static fields have unbounded lifetimes; the storage location is created at some time before the field is accessed and not destroyed until the appdomain is torn down.

Suppose two people call System.Math.Abs() at exactly the same time.

OK. How do you propose that happens?

How does the runtime differentiate between the two callers? Is this where threading comes in?

The static method is jitted into a bunch of machine instructions that are numbers in memory. Each thread of execution has a number associated with it called the instruction pointer which locates the current instruction. Two different threads can both have instruction pointers that are inside the same static method at the same time.

Are separate threads automatically created for each caller?

The question doesn't make any sense. How did you get two callers at the same time if they weren't already on separate threads?

like image 145
Eric Lippert Avatar answered Oct 19 '22 15:10

Eric Lippert