C#类和结构区别的总结(一)
2007-08-28 00:51
295 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = new int();
string a = " ";
Console.WriteLine("{0}{1}",a,i);
int[] b = new int[10];
Console.WriteLine(b[3]);
structOne soInstanceOne = new structOne ();
Console.WriteLine("soInstanceOne:{0}:{1}", soInstanceOne.filed1, soInstanceOne.filed2);
structOne[] soArrayOne = new structOne[10];
Console.WriteLine("soArrayOne[]:{0}:{1}",soArrayOne[2].filed1,soArrayOne[2].filed2 );
soArrayOne[0].people = new string[4];
soArrayOne[0].people[0] = "男";
soArrayOne[0].people[1] = "18";
soArrayOne[0].people[2] = "Jack";
foreach (string peopleJack in soArrayOne[0].people)
{
Console.WriteLine(peopleJack);
}
soInstanceOne.people = new string[3];
soInstanceOne.people[0] = "女";
soInstanceOne.people[1] = "17";
soInstanceOne.people[2] = "Alice";
foreach (string peopleAlice in soInstanceOne.people)
{
Console.WriteLine(peopleAlice);
}
Console.WriteLine(structOne.filed4);
soInstanceOne.StructOneMethodOne();
classOne Test = new classOne();
Console.WriteLine ("Test:");
Console.WriteLine ("{0}:{1}:{2}",Test.cFiled1,Test.cFiled2 ,Test.cFiled3 ); //打印0::false
Console .ReadLine ();
}
struct structOne
{
public int filed1; //执行这个语句structOne soInstanceOne = new structOne()之后, filed1的值初始化为0
public string filed2; //执行这个语句structOne soInstanceOne = new structOne()之后, filed2的值初始化为null
public string[] people;
//public sting[] people = { "男", "17", "Jack" };//结构中不能有实例字段初始值设定项,意思就是不能为实例字段赋值
//private int filed3 = 3; //不能有实例字段初始值设定项,意思就是不能为实例字段赋值
public static int filed4 = 4; //结构中可以为静态字段赋值
private const string conA = "conA"; //结构中可以为常数字段赋值,因为常数属于Static
private static string strName = "John"; //结构中可以为静态字段赋值
public static string[] anotherPeople = { "男", "17", "Tom" }; //结构中可以为静态数组赋值
public bool bResult;
public void StructOneMethodOne()
{
Console.WriteLine(conA);
Console.WriteLine(strName);
Console.WriteLine(bResult);
}
}
class classOne
{
public int cFiled1;
public string cFiled2;
public bool cFiled3;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = new int();
string a = " ";
Console.WriteLine("{0}{1}",a,i);
int[] b = new int[10];
Console.WriteLine(b[3]);
structOne soInstanceOne = new structOne ();
Console.WriteLine("soInstanceOne:{0}:{1}", soInstanceOne.filed1, soInstanceOne.filed2);
structOne[] soArrayOne = new structOne[10];
Console.WriteLine("soArrayOne[]:{0}:{1}",soArrayOne[2].filed1,soArrayOne[2].filed2 );
soArrayOne[0].people = new string[4];
soArrayOne[0].people[0] = "男";
soArrayOne[0].people[1] = "18";
soArrayOne[0].people[2] = "Jack";
foreach (string peopleJack in soArrayOne[0].people)
{
Console.WriteLine(peopleJack);
}
soInstanceOne.people = new string[3];
soInstanceOne.people[0] = "女";
soInstanceOne.people[1] = "17";
soInstanceOne.people[2] = "Alice";
foreach (string peopleAlice in soInstanceOne.people)
{
Console.WriteLine(peopleAlice);
}
Console.WriteLine(structOne.filed4);
soInstanceOne.StructOneMethodOne();
classOne Test = new classOne();
Console.WriteLine ("Test:");
Console.WriteLine ("{0}:{1}:{2}",Test.cFiled1,Test.cFiled2 ,Test.cFiled3 ); //打印0::false
Console .ReadLine ();
}
struct structOne
{
public int filed1; //执行这个语句structOne soInstanceOne = new structOne()之后, filed1的值初始化为0
public string filed2; //执行这个语句structOne soInstanceOne = new structOne()之后, filed2的值初始化为null
public string[] people;
//public sting[] people = { "男", "17", "Jack" };//结构中不能有实例字段初始值设定项,意思就是不能为实例字段赋值
//private int filed3 = 3; //不能有实例字段初始值设定项,意思就是不能为实例字段赋值
public static int filed4 = 4; //结构中可以为静态字段赋值
private const string conA = "conA"; //结构中可以为常数字段赋值,因为常数属于Static
private static string strName = "John"; //结构中可以为静态字段赋值
public static string[] anotherPeople = { "男", "17", "Tom" }; //结构中可以为静态数组赋值
public bool bResult;
public void StructOneMethodOne()
{
Console.WriteLine(conA);
Console.WriteLine(strName);
Console.WriteLine(bResult);
}
}
class classOne
{
public int cFiled1;
public string cFiled2;
public bool cFiled3;
}
}
}
相关文章推荐
- C#类和结构区别的总结(一)
- 问题二,要想进外企,英语当然要硬,其他的要看你对数据结构的理解,先总结一下。类与结构的区别
- 总结:Stuts 和Spring 两种MVC框架结构的区别
- 向方法传递结构和向方法传递引用之间的区别 总结
- C#类和结构的区别
- 总结:Linux体系结构和内核结构区别
- c#类和结构的区别
- 结构体(struct)、联合体(union)、枚举(enum) 三种结构的区别及各个结构细节的总结
- 深入c# 类和结构的区别总结详解
- pt,px,rem和em之间区别总结
- JS刷新页面总结及区别
- 结构与联合有和区别?
- Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结
- C# 接口和抽象类的区别和使用场合(知识要点归纳总结)
- 数据结构期末总结
- TCP/IP 协议簇下的各报文结构总结
- 【区别总结】C、Java和脚本语言区别
- IPV4与IPV6地头部结构的区别
- Java入门-总结下Comparable和Comparator的区别
- oc 基础知识总结三 nil、null、NULL、NSNull的区别