C # Language Specification--1.2 type

Source: Internet
Author: User
Tags define object ibase include interface variables string variable
Specification

C # supports two types: ' Value type ' and ' reference type '. Value types include simple types (such as char, int, and float), enumeration types, and struct types. Reference types include classes (Class) types, interface types, delegate types, and array types.


A value type differs from a reference type in that a variable of a value type directly contains its data, whereas a variable of a reference type stores an object reference. For reference types, two variables may reference the same object, so operations on one variable may affect the object referenced by another variable. For value types, each variable has its own copy of the data, and it is not possible for the operation of one variable to affect another variable.


Example

Using System;

Class Class1

{

public int Value = 0;

}

Class Test

{

static void Main () {

int val1 = 0;

int val2 = VAL1;

Val2 = 123;

Class1 ref1 = new Class1 ();

Class1 ref2 = ref1;

Ref2. Value = 123;

Console.WriteLine ("Values: {0}, {1}", Val1, Val2);

Console.WriteLine ("Refs: {0}, {1}", Ref1.) Value, Ref2. Value);

}

}


Shows the difference. Run the program with the following output visible:

values:0, 123

Refs:123, 123


Assigning a value to a local variable val1 does not affect the local variable val2, because two local variables are value types (int types), and each local variable holds its own data. Instead, the assignment is Ref2. Value = 123; Will affect Ref2 because Ref1 and Ref2 refer to the same object.


Line of code should be

Console.WriteLine ("Values: {0}, {1}", Val1, Val2);

Console.WriteLine ("Refs: {0}, {1}", Ref1.) Value, Ref2. Value);


For further explanation, because the method console.writeline some string formatting behavior is more complex, it requires a variable number of parameters. The first argument is a string that may contain a numbered placeholder similar to {0} and {1}. Each placeholder references a trailing parameter: {0} references the second argument, {1} references the third parameter, and so on. Before the output is sent to the console, each placeholder is replaced with the value of the parameter it references, and is displayed in the specified format.


Developers can define new value types through enumeration declarations and struct declarations, and can define new reference types through class declarations, interface declarations, and delegate declarations. Example

Using System;

public enum Color

{

Red, Blue, Green.

}

public struct Point

{

public int x, y;

}

public interface IBase

{

void F ();

}

public interface Iderived:ibase

{

void G ();

}

public class A

{

protected virtual void H () {

Console.WriteLine ("A.H");

}

}

public class b:a, iderived

{

public void F () {

Console.WriteLine ("B.f, implementation of IDERIVED.F");

}

public void G () {

Console.WriteLine ("B.G, implementation of IDERIVED.G");

}

Override protected void H () {

Console.WriteLine ("B.H, override of A.H");

}

}

public delegate void Emptydelegate ();

Examples of each type of declaration are enumerated. The following sections will clarify the details about the type declaration.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.