creating new objects in a static method

simplify3000 1 Reputation point
2022-09-16T16:30:19.45+00:00

Hi all,

I have a static method as follows:

public static class Helper  
{  
public static void WriteMessage (string message)  
{  
   ///////////////  
   var myObj = new MyObjectDetail();  
   ///////////////  
   myObj.message = message;  
   myObj.TimeStamp = DateTime.UtcNow();  
   Console.WriteLine(myObj.ToJsonFormat());  
}  
}  

this will be called frequently as:
Helper.WriteMessage("method A started"); //etc.

My question is,
By creating a new object inside every call to the static "WriteMessage" (var myObj = new MyObjectDetail(); )

  1. does that have a performance impact?
  2. is there a possibility of data used incorrectly between threads or calls, e.g. call1 writing out a message meant for call2?

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,219 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2022-09-16T16:44:47.397+00:00
    1. does that have a performance impact?

    Every code that runs has a performance impact. Since you've provided no relevant source code or your performance expectations, there is no way to answer this question.

    is there a possibility of data used incorrectly between threads or calls, e.g. call1 writing out a message meant for call2?

    Static methods are typically thread safe but you have not provided the MyObjectDetail source code so we have no idea if MyObjectDetail is thread safe.

    0 comments No comments

  2. simplify3000 1 Reputation point
    2022-09-16T17:02:44.763+00:00

    The core requirement is we no longer want to write out simple messages,
    but format each message as a Json string and then write it to a log.

    I meant is it "bad practice" to frequently call a static method that creates new instances of an object for every call.
    Would it be better to not do this and whereever I have a call like Helper.WriteMessage("method A started");

    • should I just create the new object there in each caller class, and forget about static methods.
      (would be a lot of tedious code changes).

    MyObjectDetails is a class that has about 20 properties, like below:
    Most of the properties are of no use to my code, so we only set the message and TimeStamp.
    There are no actual methods that do any processing other than getters and setters like below:

        public DateTime TimeStamp { get; set; }  
    
        public EventId EventId { get; set; }  
    
        public string ErrCode { get; set; } = string.Empty;  
    
        public long Duration { get; set; }  
    

  3. simplify3000 1 Reputation point
    2022-09-16T17:35:07.947+00:00

    Ok thank you for taking the time to answer, however I would like to ask other community members what they think.
    I'm asking this question to see if anyone else has done anything similar in their own application and may have thoughts about it.
    Other opinions are always welcomed.

    0 comments No comments

  4. Karen Payne MVP 35,031 Reputation points
    2022-09-16T20:04:55+00:00

    One idea is to use a singleton

    Define a model

    public class Container  
    {  
        public string Message { get; set; }  
        public DateTime DateTime { get; set; }  
    }  
    

    Setup a method in a singleton class

    public sealed class Singleton  
    {  
        private static readonly Lazy<Singleton> Lazy =   
            new(() => new Singleton());  
        public static Singleton Instance => Lazy.Value;  
      
        public void ToJsonFormat(Container container)  
        {  
            Console.WriteLine(JsonConvert.SerializeObject(  
                container, Formatting.Indented));  
        }  
      
    }  
    

    Use it

    Singleton.Instance.ToJsonFormat(new Container()  
    {  
        DateTime = DateTime.Now,   
        Message = "Hello"  
    });  
    
    0 comments No comments

  5. simplify3000 1 Reputation point
    2022-09-19T15:28:02.613+00:00

    great, thank you.
    The main thing I wanted to see was if anyone had the opinion of "I tried it before and it caused major problems".
    Good to see that was not reported, now I can try out the other things mentioned here.

    Thanks

    0 comments No comments