مروری بر سازندهها - سازندههای ایستا (static)
نویسنده: مجتبی چنانی
تاریخ: ۱۳۹۱/۰۴/۳۱ ۱۳:۲۴
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
class test
{
static string fname, lname;
static test()
{
console.write("first name:");
fname = console.readline();
console.write("last name:");
lname = console.readline();
console.writeline("thank you");
}
public test()
{
//some other code
}
}
با سلام
بله در یک کلاس ایستا میتوان سازندهی ایستا داشت، البته این نکته را فراموش نکنید که در کلاسهای ایستا تمامی متد ها، خصوصیات، اشیاء و... باید ایستا باشند لذا شما نمیتوانید سازنده معمولی داشته باشید.
public interface ICheckoutConfig
{
bool UseGeolocation { get; }
}
public class CheckoutConfig : ICheckoutConfig
{
static CheckoutConfig()
{
bool.TryParse(ConfigurationManager.AppSettings["UseGeolocation"], out _useGeolocation);
}
private static bool _useGeolocation;
public bool UseGeolocation
{
get { return _useGeolocation; }
}
} public class ProjectsContext : DbContext
{
//the static constructor runs a single time with the first instance of a class
static ProjectsContext()
{
Database.SetInitializer(new CreateDatabaseIfNotExists<ProjectsContext>);
}
}