日期:2014-05-16 浏览次数:20699 次
public class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { Name = name; Age = age; } } class Program { static void Main() { Person person1 = new Person("Leopold", 6); Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); // Declare new person, assign person1 to it. Person person2 = person1; //Change the name of person2, and person1 also changes. person2.Name = "Molly"; person2.Age = 16; Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age); Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
public struct Person { public string Name; public int Age; public Person(string name, int age) { Name = name; Age = age; } } public class Application { static void Main() { // Create struct instance and initialize by using "new". // Memory is allocated on thread stack. Person p1 = new Person("Alex", 9); Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); // Create new struct object. Note that struct can be initialized // without using "new". Person p2 = p1; // Assign values to p2 members. p2.Name = "Spencer"; p2.Age = 7; Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age); // p1 values remain unchanged because p2 is copy. Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }Output:
namespace ConsoleApplication5 { struct struct1 { //int x = 10; //error int x; string str; //struct1(){} //error //struct1(int _x) //error //{ x = _x; } struct1(int _x, string _str) { x = _x; str = _str; } const int y = 10; //OK static int z = 100; //OK } }