변수에 복사시 타입이 다른 경우 실행

- 암시적(implicit)변환, 데이터 손실이 없음

int intNum = 100;  long lingNum = intNum   // 작은타입 -> 큰타입 (암시적 변환가능)

long longNum = 100L;  int intNum = longNum  // 큰타입 -> 작은타입(불가능)

Parent p = new Parent(); Child c = new Child();

p = c;  //  파생 클래스에서 기본클래스로 암시적 변환 가능(파생 클래스에 항상 기본 클래스의 모든 멤버가 포함되므로 특수 구문이 필요하지 않음)

c = p;  //  기본클래스에서 파생클래스로 암시적 변환 불가능

 

- 명시적 변환(Explicit Conversion)

   : 변환시 정보손실의 위험이 있는경우, cast operator 이용하여 숫자 타입간, 클래스 타입간 변환

double a = 30.77;

int b; b = (int)a;  //  0.77의 데이터 손실 그러나 타입변환

// 파생클래스를 기본클래스에 저장, 그리고 다시 파생클래스로 형변화시에만 가능
Parent p = new Parent();   Child c = new Child();  c = (Child)p;  // InvalidCastException
Parent p = new Child();  Child c = new Child();  c = (Child)p;  // 정상작동

// 즉풀이하면,
(Child c = new Child();  Parent p = c;  Child c2 = (child)p;

 

- 사용자 정의변환(User-defined Conversion)

 

- Helper Class

// System.Convert 클래스, Parse 메소드 사용

string str = “1000”;
int num = Convert.ToInt32(str);
int num2 = Int32.Parse(str)

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

static, encapsulation and access modifier  (0) 2021.01.07
매개변수 키워드  (0) 2021.01.06
배열선언  (0) 2021.01.06
메모리구조, 가비지컬렉터  (0) 2021.01.05
ASP.NET MVC5 Form 관련  (0) 2020.11.28

- Form Tag 생성

// 퓨어 html 출력시
<form action=”/Home/CreatePerson/MyIdValue”, class=”personClass” data-formType=”person” method=”post”>

// Helper Method

#Basic
@using(Html.BeginForm()){ … }

#Parameters
@using(Html.BeginForm(action, controller, routeValues, method, attributes))

#실제 사용예시
@using(Html.BeginForm(“CreatePereson”, “Home”, new{id=”MyIdValue” }, FormMethod.Post,
     new {@class = “personClass”, data_formType=”person”})) { … }

- HIDDEN

// Form Tag
<input type=”hidden” id=”documentId” name=”documentId” value=”@Model.documentId”>

// Helper Tag
@Html.HiddenFor(model => model.documentId)

- SELECT

// Form Tag
<label for=”cities”>Select a City</label>
<select name=”cities” id=”cities”>
  <option value=”toronto”>Toronto</option>
  <option value=”london”>London</option>
  <option value=”Milton”>Milton</option>
</select>

// DDL Helper
@Html.DropDownListFor(model => model.Cities, new SelectList(new [] {
  “Toronto”, “London”, ”Milton”}), “Select”, 
  new{Class=”form-control”}
)

// DDL Helper - enum
public enum ClassType
{
  Trial = 1,
  Paid = 2
}
@Html.EnumDropDownListFor(model => model.ClassType, “Select”, new{@class=”form-control”})

- RADIO

// Form Tag
<label for=”toronto">Toronto</label>
<input type=”radio” id=”toronto" name=”cities” value=”toronto”>
<label for=”london">London</label>
<input type=”radio” id=”london" name=”cities” value=”london”>
<label for=”milton">Milton</label>
<input type=”radio” id=”milton" name=”cities” value=”milton”>

// Helper Method
public class Student
{
  public int StudentId {get; set;}
  public string StudentName {get; set;}
  public string Cities{get; set;}
}

@model Student

@Html.RadioButtonFor(model => model.Cities, “toronto")Toronto
@Html.RadioButtonFor(model => model.Cities, “london")London
@Html.RadioButtonFor(model => model.Cities, “milton")Milton

- CHECKBOX

// Form Tag
// Value가 있으면, 서브밋될때 밸류값이 전달된다.
<input type=”checkbox” id=”toronto" name=”toronto" value=”inGTA” checked>
<label for=”toronto”>Toronto</label>
<input type=”checkbox” id=”london" name=”london" value=”outGTA”>
<label for=”london”>London</label>

// Helper Method
@model Student

@Html.CheckBoxFor(model => model.Cities, new {Style = “font-size:10px}”)

- TEXT

/ Form Tag
<input type="text" id="cities" name="cities" />

/ Helper Method
@Html.TextBoxFor(model => model.cities, new {@class =”form-control”})
@Html.EditorFor(model => model.cities, new{htmlAttributes = new{@class=”form-control”}})
@Html.EditorFor(model => model.cities, new{htmlAttributes= new{Class=”form-control”, Type=”data”, Required=”required”, Readonly=”readonly”}})
@class 를 Class로 사용

- TEXTAREA

// Form Tag
<textarea id="cities" name="cities" rows="3" cols="10">

// Helper Method
@Html.TextAreaFor(model => model.cities, new{rows=”3”, cols=”10”})

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

static, encapsulation and access modifier  (0) 2021.01.07
매개변수 키워드  (0) 2021.01.06
배열선언  (0) 2021.01.06
메모리구조, 가비지컬렉터  (0) 2021.01.05
Type conversions(형변환)  (0) 2021.01.05

+ Recent posts