Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static variable shared between class instances

Tags:

c#

class

static

I have the class below. _commandLine variable is declared as static. Does this variable will be shared between WkHtml class instances?

p.s. It would be cool to have Tool which would show such information in VS. For example developer select variable and get information about variable, is variable shared between classes, is it thread safe and etc.

public class WkHtml
{
    private static string _commandLine;

    public void AddCommandLine(object value)
    {
        AddCommandLine("{0}", value);
    }

    public void AddCmdWithCond(string value, bool condition, object compareValue)
    {
        AddCmdWithCond(value, condition, compareValue, "");
    }

    public void AddCmdWithCond(string value, bool condition, object compareValue, string defaultValue)
    {
        if (compareValue != null && !string.IsNullOrEmpty(compareValue.ToString()) && Helpers.GetBool(compareValue) == condition)
            AddCommandLine("{0}", value);
        else
            if (defaultValue != null)
                AddCommandLine("{0}", defaultValue);
    }

    public void AddCommandLine(object parameter, object value, object defaultValue)
    {
        if (value == null || string.IsNullOrEmpty(value.ToString()))
        {
            value = defaultValue;
        }
        AddCommandLine(parameter, value);
    }

    public void AddCommandLine(object parameter, object value)
    {
        if (value == null || string.IsNullOrEmpty(value.ToString())) return;
        _commandLine = _commandLine + string.Format(parameter.ToString(), value) + " ";
    }

    public string GetCommandLine
    {
        get { return _commandLine; }
    }

}
like image 507
Tomas Avatar asked Dec 02 '22 01:12

Tomas


1 Answers

static variables are shared between all class instances.

I don't see any reason to why you should declare it as static?

like image 93
jgauffin Avatar answered Jan 02 '23 04:01

jgauffin