تفاوت در سه نوع متفاوت کلاس
با سلام و وقت بخیر و خسته نباشید خدمت دوستان محترم :n16:
نظر شما در رابطه با تفاوت بین Static class و Private constructor در چیست؟ ما در هر دو نمیتوانیم شی ایجاد کنیم.
پس کاربرد و زمینه استفاده هرکدام چه موقع است؟
تکلیف سازنده استاتیک Static Constructor مشخص هست. 1 بار ایجاد می شود و یا برای مقدار دهی اولیه به اعضای استاتیک به کار می رود
using System;
کد:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @private
{
static class Counter
{
public Counter() { }
public static int counter;
public static int counterMethod()
{
return ++counter;
}
}
class Program
{
static void Main(string[] args)
{
Counter.counter = 100;
Console.WriteLine( Counter.counterMethod());
Counter n = new Counter(); //We have an error
Console.ReadKey();
}
}
}
کد:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @private
{
class Counter
{
private Counter() { }
public static int counter;
public static int counterMethod()
{
return ++counter;
}
}
class Program
{
static void Main(string[] args)
{
Counter.counter = 100;
Console.WriteLine( Counter.counterMethod());
Counter n = new Counter(); //We have an error
Console.ReadKey();
}
}
}