ASP.NET Razor - C# 변수


변수는 데이터를 저장하는 데 사용되는 명명된 엔터티입니다.


변수

변수는 데이터를 저장하는 데 사용됩니다.

변수 이름은 영문자로 시작해야 하며 공백이나 예약 문자를 포함할 수 없습니다.

변수는 저장하는 데이터의 종류를 나타내는 특정 유형일 수 있습니다. 문자열 변수는 문자열 값("Welcome to W3Schools")을 저장하고, 정수 변수는 숫자 값(103)을 저장하고, 날짜 변수는 날짜 값 등을 저장합니다.

변수는 var 키워드를 사용하거나 유형을 사용하여 선언합니다. 유형), 그러나 ASP.NET은 일반적으로 데이터 유형을 자동으로 결정할 수 있습니다.

// Using the var keyword:
var greeting = "Welcome to W3Schools";
var counter = 103;
var today = DateTime.Today;

// Using data types:
string greeting = "Welcome to W3Schools";
int counter = 103;
DateTime today = DateTime.Today;

데이터 유형

다음은 일반적인 데이터 유형 목록입니다.

Type Description Examples
int Integer (whole numbers) 103, 12, 5168
float Floating-point number 3.14, 3.4e38
decimal Decimal number (higher precision) 1037.196543
bool Boolean true, false
string String "Hello W3Schools", "John"


연산자

연산자는 표현식에서 수행할 명령의 종류를 ASP.NET에 알려줍니다.

 C# 언어는 많은 연산자를 지원합니다. 다음은 일반적인 연산자 목록입니다.

Operator Description Example
= Assigns a value to a variable. i=6
+
-
*
/
Adds a value or variable.
Subtracts a value or variable.
Multiplies a value or variable.
Divides a value or variable.
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
Increments a variable.
Decrements a variable.
i += 1
i -= 1
== Equality. Returns true if values are equal. if (i==10)
!= Inequality. Returns true if values are not equal. if (i!=10)
<
>
<=
>=
Less than.
Greater than.
Less than or equal.
Greater than or equal.
if (i<10)
if (i>10)
if (i<=10)
if (i>=10)
+ Adding strings (concatenation). "w3" + "schools"
. Dot. Separate objects and methods. DateTime.Hour
() Parenthesis. Groups values. (i+5)
() Parenthesis. Passes parameters. x=Add(i,5)
[] Brackets. Accesses values in arrays or collections. name[3]
! Not. Reverses true or false. if (!ready)
&&
||
Logical AND.
Logical OR.
if (ready && clear)
if (ready || clear)

데이터 유형 변환

한 데이터 유형에서 다른 데이터 유형으로 변환하는 것이 때때로 유용합니다.

가장 일반적인 예는 문자열 입력을 정수 또는 날짜와 같은 다른 유형으로 변환하는 것입니다.

일반적으로 사용자가 숫자를 입력하더라도 사용자 입력은 문자열로 제공됩니다. 따라서 숫자 입력 값을 계산에 사용하려면 먼저 숫자로 변환해야 합니다.

다음은 일반적인 변환 방법 목록입니다.

Method Description Example
AsInt()
IsInt()
Converts a string to an integer. if (myString.IsInt())
  {myInt=myString.AsInt();}
AsFloat()
IsFloat()
Converts a string to a floating-point number. if (myString.IsFloat())
  {myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
Converts a string to a decimal number. if (myString.IsDecimal())
  {myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
Converts a string to an ASP.NET DateTime type. myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool()
Converts a string to a Boolean. myString="True";
myBool=myString.AsBool();
ToString() Converts any data type to a string. myInt=1234;
myString=myInt.ToString();