各类操作符的示例
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
var x = 100 ;
Console. WriteLine ( x. GetType ( ) . Name) ;
var y = "100" ;
}
}
}
new操作符 new操作符在内存中创建类型的实例,并且立刻调用它的实例构造器。如果new左边有赋值符号,new操作符会把自己拿到的实例内存地址通过赋值操作符交给负责访问这个实例的变量。 new操作符还可以调用实例的初始化器。可以立刻为实例的属性设置值,初始化器可以初始化多个属性
using System;
using System. Windows. Forms;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
Form myForm = new Form ( ) { Text= "Hello!" } ;
myForm. ShowDialog ( ) ;
}
}
}
using System;
using System. Windows. Forms;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
Form myForm = new Form ( ) { Text= "Hello!" } ;
myForm. ShowDialog ( ) ;
var person = new { Name = "Mr.Okay" , Age = 24 } ;
Console. WriteLine ( person. Name) ;
Console. WriteLine ( person. Age) ;
Console. WriteLine ( person. GetType ( ) . Name) ;
}
}
}
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
uint x = uint . MaxValue;
Console. WriteLine ( x) ;
string binStr = Convert. ToString ( x, 2 ) ;
Console. WriteLine ( binStr) ;
try
{
uint y = checked ( x + 1 ) ;
Console. WriteLine ( y) ;
}
catch ( OverflowException ex)
{
Console. WriteLine ( "There's overflow!" ) ;
}
}
}
}
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
uint x = uint . MaxValue;
Console. WriteLine ( x) ;
string binStr = Convert. ToString ( x, 2 ) ;
Console. WriteLine ( binStr) ;
checked
{
try
{
uint y = x + 1 ;
Console. WriteLine ( y) ;
}
catch ( OverflowException ex)
{
Console. WriteLine ( "There's overflow!" ) ;
}
}
}
}
}
sizeof操作符 默认情况下,只能用于去获取基本数据类型(结构体)的实例在内存当中所占的字节数。
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
unsafe
{
int x = sizeof ( Student) ;
Console. WriteLine ( x) ;
}
}
}
struct Student
{
int ID;
long Scroe;
}
}
->操作符 C#中指针操作、取地址操作以及用指针访问成员的操作,只能用来操作结构体类型,不能用来操作引用类型。
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
unsafe
{
Student stu;
stu. ID = 1 ;
stu. Score = 97 ;
Student* pStu = & stu;
pStu- > Score = 100 ;
Console. WriteLine ( stu. Score) ;
}
}
}
struct Student
{
public int ID;
public long Score;
}
}
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
int x = 12345678 ;
int y = ~ x;
string xStr = Convert. ToString ( x, 2 ) . PadLeft ( 32 , '0' ) ;
string yStr = Convert. ToString ( y, 2 ) . PadLeft ( 32 , '0' ) ;
Console. WriteLine ( xStr) ;
Console. WriteLine ( yStr) ;
}
}
}
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
bool b1 = true ;
bool b2 = ! b1;
Console. WriteLine ( b2) ;
}
}
}
using System;
namespace OperatorsExample
{
class Program
{
static void Main ( string [ ] args)
{
int x1 = 100 ;
int y1 = x1++ ;
Console. WriteLine ( x1) ;
Console. WriteLine ( y1) ;
int x2 = 100 ;
int y2 = ++ x2;
Console. WriteLine ( x2) ;
Console. WriteLine ( y2) ;
}
}
}