.NET中的数据类型

一 String 

String的特性

  class  Program
    
{
        
static void Main(string[] args)
        
{
            
string a = "ABC";
            
string b = "ABC";
            
string c = new string(new char[] 'A''B''C'});
            
string d = new string(new char[] 'A''B''C' });
            Console.WriteLine(a.GetHashCode());
            Console.WriteLine(b.GetHashCode());
            Console.WriteLine(c.GetHashCode());
            Console.WriteLine(d.GetHashCode());
            Console.WriteLine(
"a == b ? {0}", a == b);
            Console.WriteLine(
"(object)a == (object)b ? {0}", (object)a == (object)b);
            Console.WriteLine(
"c == d ? {0}", c == d);
            Console.WriteLine(
"(object)c == (object)d ? {0}", (object)c == (object)d);
            Console.WriteLine(
"a.Equals(b) ? {0}", a.Equals(b));
            Console.WriteLine(
"(object)a).Equals((object)b) ? {0}", ((object)a).Equals(((object)b)) );
            Console.WriteLine(
"c.Equals(d) ? {0}", c.Equals(d));
            Console.WriteLine(
"(object)c).Equals((object)d) ? {0}", ((object)c).Equals(((object)d)));

            Complex c1 
= new Complex(32);
            Complex c2 
= new Complex(32);
            Console.WriteLine(c1.GetHashCode());
            Console.WriteLine(c2.GetHashCode());
            Console.WriteLine(
"c1 == c2 ? {0}", c1 == c2);
            Console.WriteLine(
"c1.Equals(c2) ? {0}", c1.Equals(c2));
            Console.WriteLine(
"((object)c1).Equals(((object)c2)) ? {0}", ((object)c1).Equals(((object)c2)));
            Console.ReadLine();
        }

    }


    
class  Complex : IEquatable < Complex >
    
{
        
public int real;
        
public int imaginary;
        
public Complex(int real, int imaginary)
        
{
            
this.real = real;
            
this.imaginary = imaginary;
        }

        
public double Module()
        
{
            
return Math.Sqrt(Math.Pow(this.real, 2+ Math.Pow(this.imaginary, 2));
        }

        
public override string ToString()
        
{
            
return string.Format("{0} + {1}i"this.real, this.imaginary);
        }

        
implement IEquatable  interface
    }

 

1) CompareString 

  class  Program
    
{
        
static void Main(string[] args)
        
{
            
string a = "Bingosoft";
            
string b = "bingoSOFT";
            
bool flag;
            
code here

            Console.ReadLine();
        }

        
static void PrintCurrentTime()
        
{
            DateTime currentTime 
= DateTime.Now;
            Console.WriteLine(
"{0}:{1}", currentTime.ToLongTimeString(), currentTime.Millisecond);
        }

    }

2)String和StringBuilder

   class  Program
    
{
        
static void Main(string[] args)
        
{
            
// System.String Object
            string a = "Bingosoft";
            
string b = a;
           
            Console.WriteLine(
"a = b ? {0}", a.Equals(b)); // a 和 b 是否指向同一个对象?
            Console.WriteLine("a = {0} , b = {1}", a, b);
            a 
= a + " Microsoft";
            Console.WriteLine(
"a = b ? {0}", a.Equals(b)); // a 和 b 还是指向同一个对象吗?
            Console.WriteLine("a = {0} , b = {1}", a, b);
            
// System.Text.StringBuilder Object
            StringBuilder c = new StringBuilder("Bingosoft");
            StringBuilder d 
= c;
            Console.WriteLine(
"c = d ? {0}", c.Equals(d)); // c 和 d 是否指向同一个对象?
            Console.WriteLine("c = {0} , d = {1}", c, d);
            c.Append(
" Microsoft");
            Console.WriteLine(
"c = d ? {0}", c.Equals(d)); // c 和 d 还是指向同一个对象吗?
            Console.WriteLine("c = {0} , d = {1}", c, d);
            Console.ReadLine();
        }

    }

3)FormatString

class  Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
                FormatString(
"company is {0}, id:{2}, employee is {1}",
                
"Bingosoft",
                
new Employee("qiuwh""rd"),
                
100)
                );
            Console.ReadLine();
        }

        
static string FormatString(string format, params object[] arg)
        
{
            StringBuilder builder 
= new StringBuilder(format);
            
for (int i = 0; i < arg.Length; i++)
            
{
                builder.Replace(
"{" + i.ToString() + "}", arg[i].ToString());
            }

            
return builder.ToString();
        }

    }

    
class  Employee
    
{
        
public string Name;
        
public string Dept;
        
public Employee(string name, string dept)
        
{
            
this.Name = name;
            
this.Dept = dept;
        }

        
public override string ToString()
        
{
            
return string.Format("(Name:{0} Dept:{1})"this.Name, this.Dept);
        }

    }

Enum

class  Program
    
{
        
enum WeekDay { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
        [FlagsAttribute]
        
enum Colors { Red = 1, Yellow = 2, Blue = 4 };
        
static void Main(string[] args)
        
{
            WeekDay weekday 
= WeekDay.Friday;
            Console.WriteLine(weekday);
           
            Colors purple 
= Colors.Red | Colors.Blue;
            Colors orange 
= Colors.Red | Colors.Yellow;
            Colors green 
= Colors.Yellow | Colors.Blue;
            Console.WriteLine(
"purple is mixed with {0}", purple.ToString());
            Console.WriteLine(
"orange is mixed with {0}", orange.ToString());
            Console.WriteLine(
"green is mixed with {0}", green.ToString()); 
            
           
            Console.ReadLine();
        }

    }

Structure

  class  Program
    
{
        
static void Main(string[] args)
        
{
            
// Structure Demo
            EmployeeStruct emp1 = new EmployeeStruct("Daniel""R&D");
            EmployeeStruct emp2 
= emp1;
            Console.WriteLine(
"--before change value--");
            Console.WriteLine(
"emp1 = " + emp1.ToString());
            Console.WriteLine(
"emp2 = " + emp2.ToString());
            Console.WriteLine();
            emp2.Name 
= "Supersnake";
            emp2.Dept 
= "Dev2";
            Console.WriteLine(
"--after change value--");
            Console.WriteLine(
"emp1 = " + emp1.ToString());
            Console.WriteLine(
"emp2 = " + emp2.ToString());
            
// Class Demo
            EmployeeClass emp3 = new EmployeeClass("Daniel""R&D");
            EmployeeClass emp4 
= emp3;
            Console.WriteLine();
            Console.WriteLine(
"--before change value--");
            Console.WriteLine(
"emp3 = " + emp3.ToString());
            Console.WriteLine(
"emp4 = " + emp4.ToString());
            Console.WriteLine();
            emp4.Name 
= "Supersnake";
            emp4.Dept 
= "Dev2";
            Console.WriteLine(
"--after change value--");
            Console.WriteLine(
"emp3 = " + emp3.ToString());
            Console.WriteLine(
"emp4 = " + emp4.ToString());
            Console.ReadLine();
        }

    }

    
//  Employee Structure
     public   struct  EmployeeStruct
    
{
        
private string _name;
        
private string _dept;
        
public EmployeeStruct(string name, string dept)
        
{
            
this._name = name;
            
this._dept = dept;
        }

        
public string Name
        
{
            
get return this._name; }
            
set this._name = value; }
        }

        
public string Dept
        
{
            
get return this._dept; }
            
set this._dept = value; }
        }

        
public override string ToString()
        
{
           
return string.Format("Name:{0}, Dept:{1}"this._name, this._dept);
        }

    }

   
   
    
//  Employee Class
     public   class  EmployeeClass
    
{
        
private string _name;
        
private string _dept;
        
public EmployeeClass(string name, string dept)
        
{
            
this._name = name;
            
this._dept = dept;
        }

        
public string Name
        
{
            
get return this._name; }
            
set this._name = value; }
        }

        
public string Dept
        
{
            
get return this._dept; }
            
set this._dept = value; }
        }

        
public override string ToString()
        
{
            
return string.Format("Name:{0}, Dept:{1}"this._name, this._dept);
        }

    }

四  Params

class  Program
    
{
        
static void Main(string[] args)
        
{
            UseParams(
1);
            UseParams(
123);
            UseParams(
1030507090);
            Console.ReadLine();
        }

        
static void UseParams(params int[] list)
        
{
            Console.WriteLine(
"input params is :");
            
foreach (int i in list)
            
{
                Console.WriteLine(i.ToString());
            }

        }

    }

五 Ref  VS. out

class  Program
    
{
        
static void Main(string[] args)
        
{
            
int i = 100;
            
string s = "original";
            Employee emp1 
= new Employee("qiuwh""rd");
            Employee emp2 
= new Employee("yuanlh""rd");
           
            
//ValMethod(i, s, emp1, emp2);
            
// ref 使用前必须对变量赋值
            
//RefMethod(ref i, ref s, ref emp1, ref emp2);
            
// out 使用前不需要对变量赋值
            
// 但进入out方法时,所有的变量都会被清空,而在退出out方法时,要求所有的out变量都要被赋值
            OutMethod(out i, out s, out emp1, out emp2);
            Console.WriteLine(
"i={0}, s={1}, emp1={2}, emp2={3}", i, s, emp1.ToString(), emp2.ToString());
            Console.ReadLine();           
        }

        
static void ValMethod(int i, string s, Employee emp1, Employee emp2)
        
{
            i 
= 200;
            s 
= "changed";
            emp1.Dept 
= "dev2";
            emp2 
= new Employee("luoyq""rd");
        }

        
static void RefMethod(ref int i, ref string s, ref Employee emp1, ref Employee emp2)
        
{
            i 
= 200;
            s 
= "changed";
            emp1.Dept 
= "dev2";
            emp2 
= new Employee("luoyq""rd");
        }

        
static void OutMethod(out int i, out string s, out Employee emp1, out Employee emp2)
        
{
            i 
= 200;
            s 
= "changed";
            emp1 
= new Employee("new man""rd");
            emp1.Dept 
= "dev2";
            emp2 
= new Employee("luoyq""rd");
        }

    }

    
class  Employee
    
{
        
public string Name;
        
public string Dept;
        
public Employee(string name, string dept)
        
{
            
this.Name = name;
            
this.Dept = dept;
        }

        
public override string ToString()
        
{
            
return string.Format("Name:{0}, Dept:{1}"this.Name, this.Dept);
        }

    }

六 Ref VS. out Overload

class  Program
    
{
        
static void Main(string[] args)
        
{
            
int i = 1;
            SampleMethod(i);
            SampleMethod(
ref i);
            Console.ReadLine();
        }

        
static void SampleMethod(int i)
        
{
            Console.WriteLine(
"SampleMethod (int i) called.");
        }

        
static void SampleMethod(ref int i)
        
{
            Console.WriteLine(
"SampleMethod (ref int i) called.");
        }

        
//static void SampleMethod(out int i)
        
//{
        
//    Console.WriteLine("SampleMethod (out int i) called.");
        
//}
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值