PDA

View Full Version : سوال: Static Constructor



dr_csharp
جمعه 15 شهریور 1387, 12:36 عصر
سلام دوستان، کاربرد static constructor چیه ؟

مهرداد تاجدینی
جمعه 15 شهریور 1387, 13:37 عصر
سازنده های استاتیک برای initialize کردن فیلد های استاتیک موجود در کلاس بکار میرن.
این هم توضیحات MSDN در این زمینه :


A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.


Static constructors have the following properties:

A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

A static constructor cannot be called directly.

The user has no control on when the static constructor is executed in the program.

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

dr_csharp
جمعه 15 شهریور 1387, 20:59 عصر
سازنده های استاتیک برای initialize کردن فیلد های استاتیک موجود در کلاس بکار میرن.
این هم توضیحات MSDN در این زمینه :


A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.


Static constructors have the following properties:

A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

A static constructor cannot be called directly.

The user has no control on when the static constructor is executed in the program.

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
ممنون از پاسختون ولی من هنوز نمیدونم کارایی static construvtor چیه و سوالم همینه !!

mehdi.mousavi
جمعه 15 شهریور 1387, 21:34 عصر
سلام دوستان، کاربرد static constructor چیه ؟

سلام.
چنین ctor ای فقط یکبار Call میشه و به شما اجازه میده تا Initialization مورد نظرتون رو انجام بدین. بعنوان مثال، کلاسی رو در نظر بگیرید که عمل LOG کردن رو انجام بده، و شما میخواهید فقط برای اولین بار که قرار این کلاس ایجاد بشه، یه Entry مشخص رو در فایل بنویسید. مثلا:

فایل در تاریخ .... ایجاد شد.

به این ترتیب، فقط برای بار اول این Entry نوشته میشه، و دفعات بعد، با new کردن کلاس، دیگه ctor مربوطه فراخوانی نمیشه!

همونطوری که جناب تاجدینی نوشتن، مورد استفاده بعدی هم میتونه جایی باشه که شما میخواهید یه Wrapper برای کلاسهای Native اتون بنویسید. چون ctor مزبور فقط یکبار Call میشه، شما میتونید LoadLibrary رو در اون فراخوانی کنید و Bingo!

dr_csharp
شنبه 16 شهریور 1387, 08:46 صبح
سلام.
چنین ctor ای فقط یکبار Call میشه و به شما اجازه میده تا Initialization مورد نظرتون رو انجام بدین. بعنوان مثال، کلاسی رو در نظر بگیرید که عمل LOG کردن رو انجام بده، و شما میخواهید فقط برای اولین بار که قرار این کلاس ایجاد بشه، یه Entry مشخص رو در فایل بنویسید. مثلا:

فایل در تاریخ .... ایجاد شد.

به این ترتیب، فقط برای بار اول این Entry نوشته میشه، و دفعات بعد، با new کردن کلاس، دیگه ctor مربوطه فراخوانی نمیشه!

همونطوری که جناب تاجدینی نوشتن، مورد استفاده بعدی هم میتونه جایی باشه که شما میخواهید یه Wrapper برای کلاسهای Native اتون بنویسید. چون ctor مزبور فقط یکبار Call میشه، شما میتونید LoadLibrary رو در اون فراخوانی کنید و Bingo!
هربار که static constructor فراخوانی میشه بعنوان خروجی یک instance از کلاس بر میگرده،پس در اصل با هربار فراخوانی constructor کلاس هم فرخونی میشه دیگه !!!؟

mehdi.mousavi
شنبه 16 شهریور 1387, 10:48 صبح
هربار که static constructor فراخوانی میشه بعنوان خروجی یک instance از کلاس بر میگرده،پس در اصل با هربار فراخوانی constructor کلاس هم فرخونی میشه دیگه !!!؟

پاسخی رو که دادم، لطفا مجددا بدقت بخونید.

dr_csharp
شنبه 16 شهریور 1387, 14:50 عصر
پاسخی رو که دادم، لطفا مجددا بدقت بخونید.
یعنی برای بار اول که static constructor فراخوانی میشه و نمونه ای ازکلاس ساخته میشه constructor کلاس فراخوانی میشه و در دفعات بعدی به constructor کاری نداره ؟

mehdi.mousavi
شنبه 16 شهریور 1387, 15:03 عصر
یعنی برای بار اول که static constructor فراخوانی میشه و نمونه ای ازکلاس ساخته میشه constructor کلاس فراخوانی میشه و در دفعات بعدی به constructor کاری نداره ؟

دقیقا، دفعات بعدی با ctor ای که بصورت static تعریف شده، کاری نداره.

dr_csharp
شنبه 16 شهریور 1387, 18:56 عصر
دقیقا، دفعات بعدی با ctor ای که بصورت static تعریف شده، کاری نداره.
کلاس زیر رو درنظر بگیرید :


publicclass Student
{
private Student()
{

}
privatestring m_Name;
privatestring m_Family;
privatestring m_Code;
publicstring Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
publicstring Family
{
get
{
return m_Family;
}
set
{
m_Family = value;
}
}
publicstring Code
{
get
{
return m_Code;
}
set
{
m_Code = value;
}
}
publicstatic Student FromFile(string fileName)
{
if(!File.Exists(fileName))
returnnull;
Student student = new Student();
StreamReader reader = newStreamReader(fileName);
student.Code = reader.ReadLine();
student.Name = reader.ReadLine();
student.Family = reader.ReadLine();
reader.Close();
return student;
}
}

آیا در کلاس فوق با هربار فراخوانی متد FromFile تنها دفعه ی اول constructor کلاس فراخوانی میشه ؟

mehdi.mousavi
شنبه 16 شهریور 1387, 22:04 عصر
کلاس زیر رو درنظر بگیرید :
آیا در کلاس فوق با هربار فراخوانی متد FromFile تنها دفعه ی اول constructor کلاس فراخوانی میشه ؟

این چه ربطی به موضوع بحث ما داره؟ ما داریم در مورد Constructor های static صحبت میکنیم، و کد شما فاقد یه static ctor هستش!!!

dr_csharp
یک شنبه 17 شهریور 1387, 08:55 صبح
این چه ربطی به موضوع بحث ما داره؟ ما داریم در مورد Constructor های static صحبت میکنیم، و کد شما فاقد یه static ctor هستش!!!
دوست عزیز، وقتی constructor کلاس private باشه و امکان ساخت instance در خارج از کلاس وجود نداشته باشه،به متد static که یک instance از کلاس رو برمیگردونه Static constructor اطلاق میشه.

mehdi.mousavi
یک شنبه 17 شهریور 1387, 12:00 عصر
دوست عزیز، وقتی constructor کلاس private باشه و امکان ساخت instance در خارج از کلاس وجود نداشته باشه،به متد static که یک instance از کلاس رو برمیگردونه Static constructor اطلاق میشه.

خیر. static constructor اونی هستش که در پستهای قبلی بهش اشاره شد. این چیزی که شما بهش میگید Static Constructor در واقع اسمش هست Factory Method. وظیفه Factory Method ایجاد یک نمونه از کلاس مورد نظر هست. برای اطلاعات بیشتر در این مورد Creational Patterns رو مطالعه کنید.