Reputation: 79
Is it okay to write a class like this:
public class AlexaApi
{
public string CreateRandomPhrase()
{
return "Hello World" //There's more code, but needless for my question
}
public object ResponseBuilder(string text)
{
//CODE HERE
}
}
But then when I use the class, call "new" each Time I want to access its functions.
new AlexaApi().ResponseBuilder(new AlexaApi().CreateRandomPhrase());
I understand that this creates two instances of the AlexaApi object and it most likely should be written like:
var alexa = new AlexaApi();
alexa.ResponseBuilder(alexa.CreateRandomPhrase());
Would it use up more memory calling "new" each time? What is the worst thing that can happen? Is it really bad form to do what is written in the first example?
Upvotes: 0
Views: 83
Reputation: 29752
There's a fair chance that what you actually want is a static
method:
public class AlexaApi
{
public static string CreateRandomPhrase()
{
return "Hello World" //There's more code, but needless for my question
}
public static object ResponseBuilder(string text)
{
//CODE HERE
}
}
you can then call these methods thus:
AlexaApi.CreateRandomPhrase();
..etc.
i.e. you avoid instanciating a new class each time.
But be aware that static
methods and classes have slightly different symantics to none static
ones. Particuarly
You don't show details for what this class does so it's hard to be explicit. If you methods are just "logic", then there is no harm in making them static. If they load resources, etc. then don't.
For more info see Static vs non-static class members
Also note a class that only contains static
methods can itself be made static
. This just prevents you declaring none static methods/properties
public static class AlexaApi
{
public static string CreateRandomPhrase()
{
return "Hello World" //There's more code, but needless for my question
}
public static object ResponseBuilder(string text)
{
//CODE HERE
}
}
Upvotes: 2
Reputation: 146
As you mentioned in your question, using new instances of the class each time will take up more memory. Depending on the scope where you're calling this method and the size of the class (how many instance variables, not lines of code) this practically could amount to almost no extra memory (ram storing the extra instance of the class) or CPU time (creating the class with any default values/constructors you have in place) to eating up more memory than the rest of your program combined.
As mentioned in the comments and an answer that was posted while I was typing this, you could always make those methods static
, which means they can be called without using instances of the class at all and will save you the trouble.
Upvotes: 0
Reputation: 7204
keyword static
should fit your case:
public class AlexaApi
{
public static string CreateRandomPhrase()
{
return "Hello World" //There's more code, but needless for my question
}
public static object ResponseBuilder(string text)
{
//CODE HERE
}
}
and call directly the method without creating instance :
AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());
Upvotes: 0
Reputation: 2423
To do all that without creating multiple instances you should static methods as has been suggested. Consider the following code:
public class AlexaApi
{
public static string CreateRandomPhrase()
{
return "Hello World" //There's more code, but needless for my question
}
public static object ResponseBuilder(string text)
{
//CODE HERE
}
}
This way you can get the same results using code such as:
AlexaApi.ResponseBuilder(AlexaApi.CreateRandomPhrase());
Upvotes: 0