2

Please consider this code:

public static int ToInt (this string str)
{
    return Convert.ToInt32 (str);
}

Should I use lock for this statment?


EDIT 1)

public static int ToInt(this string str)
{
    int Id = -1;
    if (str.IsEmpty() == true ||
        int.TryParse(str.Trim().Replace(",", ""), out Id) == false)
    {
        throw new Exception("Invalid Parameter: " + str);
    }
    else
    {
        return Id;
    }         
}

is this method thread-sate too?

Arian
  • 12,793
  • 66
  • 176
  • 300
  • Why don't you simply read the documentation of the class in question - [Convert Class - Thread Safety](https://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx#Thread Safety) – Ivan Stoev Dec 19 '17 at 20:39
  • 3
    It might be interesting to know why would you think this might be not thread safe (since it seems you have some gaps in understanding what "thread safety" is about). – Evk Dec 19 '17 at 20:53
  • @IvanStoev Please consider my Edit... – Arian Dec 20 '17 at 06:19

1 Answers1

6

No, a lock is not necessary.

string is immutable; and so another thread cannot change its contents while you are trying to parse it.

It really doesn't have anything to do with extension methods; those may or may not be thread-safe based on what they do (or what parameters they take).

Besides; unless the lock was respected elsewhere in the code; doing so wouldn't change anything... (again, at least for this method)

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • What types are not immutable? – Arian Dec 19 '17 at 20:29
  • @Arian The vast majority of them. Value types, string, and `struct`s are immutable – BradleyDotNET Dec 19 '17 at 20:29
  • You mean claaes, object, lists, datatables...? – Arian Dec 19 '17 at 20:31
  • 2
    @BradleyDotNET `struct`s are *recommended* to be immutable, usually. Though `System.ValueTuple` belies that. – Kirk Woll Dec 19 '17 at 20:32
  • Yeah; lets go with structs are *mostly* immutable. You have to try to make them otherwise – BradleyDotNET Dec 19 '17 at 20:33
  • since struct _is_ value type, it's strange to agree that struct can be mutable and then say you are sure value types are not. – Evk Dec 19 '17 at 20:41
  • *No, value types are not immutable by definition. Both structs and classes can be either mutable or immutable.* https://stackoverflow.com/questions/868411/are-value-types-immutable-by-definition – Salah Akbari Dec 19 '17 at 20:42
  • 3
    not only can you not say that all value types are immutable, you also can't say that all classes are mutable. Both classes and structs can be immutable or mutable. Mutability has nothing to do with whether a type is a reference type or a value type. – Servy Dec 19 '17 at 20:42
  • @Servy, Sorry; when I said value types I should have said "primitive" types (the numerics, bool, etc). And yes; classes can be immutable but very rarely are. – BradleyDotNET Dec 19 '17 at 20:44
  • 2
    @BradleyDotNET C# has no definition for "primitive" types. It's not a well defined term in the language. Plenty of classes are immutable, in fact the one being discussed here is an immutable classes. You might make them rarely, but that's not true of everyone, and regardless, doesn't make the statement that objects are all very mutable any less wrong. – Servy Dec 19 '17 at 20:46
  • @Servy Fair enough; on the other hand in the *general* case the statement is true. I was not as precise as possible however. I agree, we don't have a good term for the "primitive" or "keyword" types (hence my parenthetical) – BradleyDotNET Dec 19 '17 at 20:48
  • `Type` has `IsPrimitive` flag, documentaiton of which lists all types that are considered primitive, so there is such term (in .NET Framework, not in C# language itself). – Evk Dec 19 '17 at 20:55
  • @Evk And VB has a definition of Primitive too, it's just an entirely different set of types, and the CLR has a set of types it calls "primitives", but it's different than the .NET or VB set. So not only is there no definition in the context of C#, but there are numerous other, conflicting, definitions in multiple other semi-related areas, so if someone uses the term they need to be specific about whether they're referring to .NET primitives, or VB primitives, or CLR primitives, or some generic concept of "primitives" for programming in general (because there are such definitions). – Servy Dec 19 '17 at 23:09
  • @Arian Same thing, nothing can come in and modify the state of any part of that method after it is called so it is thread safe – BradleyDotNET Dec 20 '17 at 15:55