© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
9 replies
ChezZ

Question regarding thread safety

Is it safe to call code on the foo object from multiple threads in this scenario? Like what happens if two threads try to access the foo object to call DoSomething() on instanceA at the same time?
static void Main()
    {
        var foo = new Foo();
        foo.instanceA = new Bar();
        foo.instanceB = new Bar();
        
        //Assuming every method on Bar is thread safe is it safe to access the foo object to call a method on Bar from any thread?
                foo.instanceA.DoSomething();
                
        
    }
    public class Foo
    {
        public Bar instanceA;
        public Bar instanceB;
    }
    
    public class Bar
    {
        public void DoSomething()
        {
            
        }
    }
static void Main()
    {
        var foo = new Foo();
        foo.instanceA = new Bar();
        foo.instanceB = new Bar();
        
        //Assuming every method on Bar is thread safe is it safe to access the foo object to call a method on Bar from any thread?
                foo.instanceA.DoSomething();
                
        
    }
    public class Foo
    {
        public Bar instanceA;
        public Bar instanceB;
    }
    
    public class Bar
    {
        public void DoSomething()
        {
            
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Question regarding tasks
C#CC# / help
2y ago
✅ Question regarding EntityFramework
C#CC# / help
3y ago
Question regarding learning.. stuff.
C#CC# / help
3y ago
Thread safety when mutating an object
C#CC# / help
10mo ago