static 키워드

static 붙은 클래스, 함수, 변수는 별도의 인스턴스 생성없이 호출가능

 

캡슐화

정보를 은닉한다는 의미로써, 아무나 객체안의 변수나 함수에 함부로 접근하지 못하도록하기위해 내부 멤버를 숨기는 것. 접근제한자를 이용

 

접근제한자

public: 어디서나 접근가능

private: 클래스내에서만 접근가능

protected: 클래스내, 파생클래스에서만 접근가능

internal: 동일 어셈블리 내에서 접근가능

protected internal: 동일 어셈블리 내에서 또는 다른 어셈블리의 파생클래스에서 접근가능

 

OOP 표준으로는 멤버변수는 private, 외부에서 호출할 함수는 public, 그외에는 protected 설정

'ASP.NET C#' 카테고리의 다른 글

상속, abstract, interface, sealed, this, base  (0) 2021.01.12
상속, abstract, virtual, new, override  (0) 2021.01.08
매개변수 키워드  (0) 2021.01.06
배열선언  (0) 2021.01.06
메모리구조, 가비지컬렉터  (0) 2021.01.05

ref, out, params

- ref: 외부에서 변수 초기화, 함수내에서 변수를 업데이트하면 외부에서도 변함

- out: 외부에서 변수 초기화 하지않음. 외부에서 선언한 변수의 값은 모두 무시됨, 함수내부의 여러값을 리턴할때 사용

- params: 매개변수를 가변성있게 만듬

using System;

namespace Example
{
	public class Program
	{
		private static int RefMethod(ref int a, int b)
		{
			a *= 100;
			b *= 100;
			return b;
		}
		private static void FlexMethod(out int a, out string msg)
		{
			a = 100;
			msg = "Done";
		}
		
		public static void Main(string[] args)
		{
			int a = 3;
			int b = 7;
			string msg = "Ready";
			
			// RefMethod의 ref를 통해 외부선언변수의 초기화가 이루어짐
			int refValue = RefMethod(ref a, b);
			Console.WriteLine("RefMethod return = {0}", refValue);
			Console.WriteLine("a = {0}", a);
			Console.WriteLine("b = {0}", b);
			
			// FlexMethod 를 통해 a, msg 값이 초기화 됨, 복수로 리턴함
			FlexMethod(out a, out msg);
			Console.WriteLine("a = {0}", a);
			Console.WriteLine("msg = {0}", msg);
			
			Console.WriteLine("Press any key");
			Console.ReadLine();
		}
	}
}	
RefMethod return = 700  // 함수의 리턴값
a = 300  // 외부에서도 값이 변함
b = 7  // 외부(변수)에서의 값 변화 없음

a = 100  // 외부값무시, 새롭게 초기화됨
msg = Done  // 외부값무시, 새롭게 초기화됨
public static void paramsMethod(params int[] list)
paramsMethod(1,2,3);
paramsMethod(1,2,3,4,5,6,7);

public static void paramsMethod(params string[] args)
paramsMethod("hello", "world");
paramsMethod("helloo", "my", "world");

public static void paramsMethod(params object[] obj)
paramsMethod("this year", "is", 2021, "last year", "is", 2020);

'ASP.NET C#' 카테고리의 다른 글

상속, abstract, virtual, new, override  (0) 2021.01.08
static, encapsulation and access modifier  (0) 2021.01.07
배열선언  (0) 2021.01.06
메모리구조, 가비지컬렉터  (0) 2021.01.05
Type conversions(형변환)  (0) 2021.01.05

C#에서의 배열 선언하기

string[] arr = {"my", "name", "is", "John"};
double[] arr = {1.1, 2.2, 3.3};
int[] arr = new int[3];
int[] arr = new int[]{1, 2, 3, 4};

 

JavsScript와 비교

string[] arr = {"my", "name", "is", "John"} var arr = ["my", "name", "is", "John"]
double[] arr = {1.1, 2.2, 3.3} var arr = [1.1, 2.2, 3.3]
int[] arr = new int[3] var arr = new Array(3)
int[] arr = new int[]{1,2,3,4} var arr - new Array(1,2,3,4)

 

'ASP.NET C#' 카테고리의 다른 글

static, encapsulation and access modifier  (0) 2021.01.07
매개변수 키워드  (0) 2021.01.06
메모리구조, 가비지컬렉터  (0) 2021.01.05
Type conversions(형변환)  (0) 2021.01.05
ASP.NET MVC5 Form 관련  (0) 2020.11.28
영역 메모리주소 크기 구성 비고
code lower 파일사이즈 program code, 상수 CPU가 명령어를 하나씩 처리함
data low 파일사이즈 global, static 변수 프로그램시작부터 종료까지 존재
heap high runtime시 결정 참조형변수, 참조값 동적할당, new키워드를 통한 인스턴스생성, 가비지컬렉터를 통한 해제
stack higher complile시 결정 값형식변수(매개변수,지역변수), 참조주소 FIFO, 함수호출에서 완료시까지 존재

 

'ASP.NET C#' 카테고리의 다른 글

static, encapsulation and access modifier  (0) 2021.01.07
매개변수 키워드  (0) 2021.01.06
배열선언  (0) 2021.01.06
Type conversions(형변환)  (0) 2021.01.05
ASP.NET MVC5 Form 관련  (0) 2020.11.28

+ Recent posts