Instance vs Static Method

Static Method (일면 Class Method)
-메서드 선언에 static 한정자가 있는 경우 해당 메서드를 정적 메서드라고 한다.
-정적 메서드는 class method나 static member field 조작을 위한 메소드이다.
-정적 메서드 내에서 클래스의 instance field나 method는 접근 불가능하다.
-정적 메서드는 특정 인스턴스에서는 작동되지 않으므로, 정적 메서드 내에서 this를 참조하면 컴파일 타임 오류가 발생한다.

Instance Method
-인스턴스 메서드는 클래스의 지정된 인스턴스에서 작동한다.
-인스턴스 메서드 내에서는 this로 액세스할 수 있다.

class ValueClass
{
private int value = 0;
public void setValue(int value) { this.value = value; }
public int getValue() { return this.value; }
public void print() { Console.WriteLine(“value={0}”, this.value);
public static void printString(string str) { Console.WriteLine(str); }
}
class Program
{
static void main(string[] args)
{
ValueClass c = new ValueClass();
c.setValue(10); // 인스턴스 메소드
c.print(); // 인스턴스 메소드
ValueClass.printString(“test”); // 정적 메소드 (클래스명.정적메소드명)
}
}

 

BankAccount Class

class BankAccount
{

double balance; // instance field
string name; // instace field
static double interest; // static field

public BankAccount() : this(1000, “HCI”) // default constructor
{
}

public BankAccount(double balance, string name) // constructor
{

this.balance = balance;
this.name = name;
BankAccount.interest = 0.7; // 0.7%

}

public void Deposit(double amount) // instance method
{

balance += amount;

}

public void Withdrawal(double amount) // instance method
{

balance -= amount;

}

public void Print() // instance method
{

double currentBalance = balance + balance * BankAccount.interest * 0.01;

Console.WriteLine(“{0}의 계좌 잔고는 {1}”, name, currentBalance);

}

public static void SetInterestRate(double interest) // static method
{

BankAccount.interest = interest;

}

}

 

class Program
{

static void Main(string[] args)
{

BankAccount b = new BankAccount(5000, “HCI222222222222”);
b.Print();

b.Deposit(1000);
b.Print();

b.Withdrawal(500);
b.Print();

BankAccount.SetInterestRate(1.5); // 1.5%
b.Print();

BankAccount b2 = new BankAccount();
b2.Print();

}

}

Local Variable vs Field vs Method

Local variables in C# must be initialized before they are used.

https://msdn.microsoft.com/en-us/library/vstudio/s1ax56ch(v=vs.100).aspx

class Test
{

static int test1; // static field test1 = 0 Warning CS0414: The field ‘Test.test1’ is assigned but its value is never used

int test2; // instance field test2 = 0 Warning CS0649: The field ‘Test.test2’ is never assigned to, and will always have its default value 0

public void Foo() // instance method
{

int test3;                 // Declaration only; Not initialized warning CS0168: The variable ‘test3’ is declared but never used
int test4 = test2;         // Ok
//int test5 = test3;         // Error CS0165: Use of unassigned local variable ‘test3’

}

public static void Foo2() // static method
{

test1 = 2;

}

}

class Program
{

static void Main(string[] args)
{

            // Declaration only
            float temperature; // warning CS0168: The variable ‘temperature’ is declared but never used
            string name; // warning CS0168: The variable ‘name’ is declared but never used
            Test myClass; // The variable ‘myClass’ is declared but never used

            // Declaration with initializers 
            double limit = 3.5;
            int[] source = { 0, 1, 2, 3, 4, 5 };
            var query = from item in source
                        where item <= limit
                        select item;

Test.Foo2(); // Static method
// Test.Foo();  // Error CS0120: An object reference is required for the non-static field, method, or property 

}

}