Reputation: 5801
Example:
public class EmailBusinessLogic
{
#region Fields and Constructors
SmtpClient smtp;
Parameter prm;
public EmailBusinessLogic()
{
prm = CostHelper.GetParameter();
smtp = new SmtpClient(prm.EmailHost, prm.EmailPort);
smtp.UseDefaultCredentials = prm.EmailUseDefaultCredentials;
smtp.DeliveryMethod = GetDeliveryMethod(prm.EmailDeliveryMethod); //CALL TO METHOD DOWN BELOW, IS THIS A GOOD PRACTICE?
smtp.EnableSsl = prm.EmailEnableSSL;
smtp.Credentials = new NetworkCredential(prm.AppUserName, prm.AppPass, prm.AppNetworkDomain);
}
#endregion
#region Instance Methods
public SmtpDeliveryMethod GetDeliveryMethod(string name)
{
switch (name)
{
case "Network": return SmtpDeliveryMethod.Network;
case "IISDirectory": return SmtpDeliveryMethod.PickupDirectoryFromIis;
case "OtherDirectory": return SmtpDeliveryMethod.SpecifiedPickupDirectory;
default: throw new NonExistentObjectException();
}
}
I am asking this because its a small paradox, i know the constructor always gets called first when instantiating this class into a new object. And i would not want to make it a static method because that will create a static instance that does not get garbage collected.
If i am wrong please correct me, a clear answer from an experienced fellow programmer is highly welcome. Thank you.
Upvotes: 1
Views: 168
Reputation: 1062855
Calling a non-static method in the constructor can be ok - but you should avoid it for virtual methods, because if the method is overridden the actual method will be called on a type that hasn't had the type-level constructor invoked yet - can cause problems.
Calling a static method would be more reliable. I think your reasons for not wanting a static method are a bit confused, and do not apply. There is absolutely no reason to avoid a static method. A static method is not implemented as an instance method on a "static instance that does not get garbage collected". There is no instance for a static method.
Upvotes: 8