c#简单教程2
来源:互联网 发布:windows常见界面开发 编辑:程序博客网 时间:2024/06/05 04:55
接受来自用户的值
System 命名空间中的 Console 类提供了一个函数 ReadLine(),用于接收来自用户的输入,并把它存储到一个变量中。例如:
int num;num = Convert.ToInt32(Console.ReadLine());
函数 Convert.ToInt32() 把用户输入的数据转换为 int 数据类型,因为 Console.ReadLine() 只接受字符串格式的数据。
C# 常量
常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。
常量可以被当作常规的变量,只是它们的值在定义后不能被修改。
定义常量
常量是使用 const 关键字来定义的 。定义一个常量的语法如下:
const <data_type> <constant_name> = value;
下面的代码演示了如何在程序中定义和使用常量:
using System;namespace DeclaringConstants{ class Program { static void Main(string[] args) { const double pi = 3.14159; // 常量声明 double r; Console.WriteLine("Enter Radius: "); r = Convert.ToDouble(Console.ReadLine()); double areaCircle = pi * r * r; Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
Enter Radius: 3Radius: 3, Area: 28.27431
其他运算符
下表列出了 C# 支持的其他一些重要的运算符,包括 sizeof、typeof 和 ? :。
StringReader r = obj as StringReader;
实例
using System;namespace OperatorsAppl{ class Program { static void Main(string[] args) { /* sizeof 运算符的实例 */ Console.WriteLine("int 的大小是 {0}", sizeof(int)); Console.WriteLine("short 的大小是 {0}", sizeof(short)); Console.WriteLine("double 的大小是 {0}", sizeof(double)); /* 三元运算符符的实例 */ int a, b; a = 10; b = (a == 1) ? 20 : 30; Console.WriteLine("b 的值是 {0}", b); b = (a == 10) ? 20 : 30; Console.WriteLine("b 的值是 {0}", b); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
int 的大小是 4short 的大小是 2double 的大小是 8b 的值是 30b 的值是 20
C# 封装
封装 被定义为"把一个或多个项目封闭在一个物理的或者逻辑的包中"。在面向对象程序设计方法论中,封装是为了防止对实现细节的访问。
抽象和封装是面向对象程序设计的相关特性。抽象允许相关信息可视化,封装则使开发者实现所需级别的抽象。
C# 封装根据具体的需要,设置使用者的访问权限,并通过 访问修饰符 来实现。
一个 访问修饰符 定义了一个类成员的范围和可见性。C# 支持的访问修饰符如下所示:
- Public:所有对象都可以访问;
- Private:对象本身在对象内部可以访问;
- Protected:只有该类对象及其子类对象可以访问
- Internal:同一个程序集的对象可以访问;
- Protected internal:一个程序集内的对象,或者该类对象及其子类可以访问;
Protected 访问修饰符
Protected 访问修饰符允许子类访问它的基类的成员变量和成员函数。这样有助于实现继承。我们将在继承的章节详细讨论这个。更详细地讨论这个。
Internal 访问修饰符
Internal 访问说明符允许一个类将其成员变量和成员函数暴露给当前程序中的其他函数和对象。换句话说,带有 internal 访问修饰符的任何成员可以被定义在该成员所定义的应用程序内的任何类或方法访问。
在上面的实例中,请注意成员函数 GetArea() 声明的时候不带有任何访问修饰符。如果没有指定访问修饰符,则使用类成员的默认访问修饰符,即为 private。
Protected Internal 访问修饰符
Protected Internal 访问修饰符允许在本类,派生类或者包含该类的程序集中访问。这也被用于实现继承。
按引用传递参数
public void swap(ref int x, ref int y) { int temp; temp = x; /* 保存 x 的值 */ x = y; /* 把 y 赋值给 x */ y = temp; /* 把 temp 赋值给 y */ }
按输出传递参数
return 语句可用于只从函数中返回一个值。但是,可以使用 输出参数 来从函数中返回两个值。输出参数会把方法输出的数据赋给自己,其他方面与引用参数相似。
public void getValue(out int x ) { int temp = 5; x = temp; }
public void getValues(out int x, out int y ) { Console.WriteLine("请输入第一个值: "); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第二个值: "); y = Convert.ToInt32(Console.ReadLine()); }
C#可空类型:
// 下面代码也可以这样子定义int? value=1; Nullable<int> value = 1;
Null 合并运算符( ?? )
Null 合并运算符用于定义可空类型和引用类型的默认值。Null 合并运算符为类型转换定义了一个预设值,以防可空类型的值为 Null。Null 合并运算符把操作数类型隐式转换为另一个可空(或不可空)的值类型的操作数的类型。
如果第一个操作数的值为 null,则运算符返回第二个操作数的值,否则返回第一个操作数的值。下面的实例演示了这点:
using System;namespace CalculatorApplication{ class NullablesAtShow { static void Main(string[] args) { double? num1 = null; double? num2 = 3.14157; double num3; num3 = num1 ?? 5.34; Console.WriteLine("num3 的值: {0}", num3); num3 = num2 ?? 5.34; Console.WriteLine("num3 的值: {0}", num3); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
num3 的值: 5.34num3 的值: 3.14157
声明数组
在 C# 中声明一个数组,您可以使用下面的语法:
datatype[] arrayName;
其中,
- datatype 用于指定被存储在数组中的元素的类型。
- [ ] 指定数组的秩(维度)。秩指定数组的大小。
- arrayName 指定数组的名称。
例如:
double[] balance;
初始化数组
声明一个数组不会在内存中初始化数组。当初始化数组变量时,您可以赋值给数组。
数组是一个引用类型,所以您需要使用 new 关键字来创建数组的实例。
例如:
double[] balance = new double[10];
赋值给数组
您可以通过使用索引号赋值给一个单独的数组元素,比如:
double[] balance = new double[10];balance[0] = 4500.0;
您可以在声明数组的同时给数组赋值,比如:
double[] balance = { 2340.0, 4523.69, 3421.0};
您也可以创建并初始化一个数组,比如:
int [] marks = new int[5] { 99, 98, 92, 97, 95};
在上述情况下,你也可以省略数组的大小,比如:
int [] marks = new int[] { 99, 98, 92, 97, 95};
您也可以赋值一个数组变量到另一个目标数组变量中。在这种情况下,目标和源会指向相同的内存位置:
int [] marks = new int[] { 99, 98, 92, 97, 95};int[] score = marks;
当您创建一个数组时,C# 编译器会根据数组类型隐式初始化每个数组元素为一个默认值。例如,int 数组的所有元素都会被初始化为 0。
访问数组元素
元素是通过带索引的数组名称来访问的。这是通过把元素的索引放置在数组名称后的方括号中来实现的。例如:
double salary = balance[9];
/* 输出每个数组元素的值 */ foreach (int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); }
String 类的属性
String 类有以下两个属性:
在当前 String 对象中获取 Char 对象的指定位置。2Length
在当前的 String 对象中获取字符数。
String 类的方法
String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。2public static int Compare( string strA, string strB, bool ignoreCase )
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。但是,如果布尔参数为真时,该方法不区分大小写。3public static string Concat( string str0, string str1 )
连接两个 string 对象。4public static string Concat( string str0, string str1, string str2 )
连接三个 string 对象。5public static string Concat( string str0, string str1, string str2, string str3 )
连接四个 string 对象。6public bool Contains( string value )
返回一个表示指定 string 对象是否出现在字符串中的值。7public static string Copy( string str )
创建一个与指定字符串具有相同值的新的 String 对象。8public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count )
从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。9public bool EndsWith( string value )
判断 string 对象的结尾是否匹配指定的字符串。10public bool Equals( string value )
判断当前的 string 对象是否与指定的 string 对象具有相同的值。11public static bool Equals( string a, string b )
判断两个指定的 string 对象是否具有相同的值。12public static string Format( string format, Object arg0 )
把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。13public int IndexOf( char value )
返回指定 Unicode 字符在当前字符串中第一次出现的索引,索引从 0 开始。14public int IndexOf( string value )
返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。15public int IndexOf( char value, int startIndex )
返回指定 Unicode 字符从该字符串中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。16public int IndexOf( string value, int startIndex )
返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。17public int IndexOfAny( char[] anyOf )
返回某一个指定的 Unicode 字符数组中任意字符在该实例中第一次出现的索引,索引从 0 开始。18public int IndexOfAny( char[] anyOf, int startIndex )
返回某一个指定的 Unicode 字符数组中任意字符从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。19public string Insert( int startIndex, string value )
返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。20public static bool IsNullOrEmpty( string value )
指示指定的字符串是否为 null 或者是否为一个空的字符串。21public static string Join( string separator, string[] value )
连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素。22public static string Join( string separator, string[] value, int startIndex, int count )
连接接一个字符串数组中的指定位置开始的指定元素,使用指定的分隔符分隔每个元素。23public int LastIndexOf( char value )
返回指定 Unicode 字符在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。24public int LastIndexOf( string value )
返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。25public string Remove( int startIndex )
移除当前实例中的所有字符,从指定位置开始,一直到最后一个位置为止,并返回字符串。26public string Remove( int startIndex, int count )
从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。27public string Replace( char oldChar, char newChar )
把当前 string 对象中,所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,并返回新的字符串。28public string Replace( string oldValue, string newValue )
把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。29public string[] Split( params char[] separator )
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。30public string[] Split( char[] separator, int count )
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。int 参数指定要返回的子字符串的最大数目。31public bool StartsWith( string value )
判断字符串实例的开头是否匹配指定的字符串。32public char[] ToCharArray()
返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。33public char[] ToCharArray( int startIndex, int length )
返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,从指定的索引开始,直到指定的长度为止。34public string ToLower()
把字符串转换为小写并返回。35public string ToUpper()
把字符串转换为大写并返回。36public string Trim()
移除当前 String 对象中的所有前导空白字符和后置空白字符。
上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。
实例
下面的实例演示了上面提到的一些方法:
比较字符串
using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string str1 = "This is test"; string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal."); } Console.ReadKey() ; } }}
当上面的代码被编译和执行时,它会产生下列结果:
This is test and This is text are not equal.
字符串包含字符串:
using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) { Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } }}
当上面的代码被编译和执行时,它会产生下列结果:
The sequence 'test' was found.
获取子字符串:
using System; namespace StringApplication {class StringProg { static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23); Console.WriteLine(substr); Console.ReadKey() ; } } }
运行实例 »
当上面的代码被编译和执行时,它会产生下列结果:
Last night I dreamt of San PedroSan Pedro
连接字符串:
using System;namespace StringApplication{ class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"}; string str = String.Join("\n", starray); Console.WriteLine(str); Console.ReadKey() ; } }}
当上面的代码被编译和执行时,它会产生下列结果:
Down the way nights are darkAnd the sun shines daily on the mountain topI took a trip on a sailing shipAnd when I reached JamaicaI made a stop
C# 结构的特点
您已经用了一个简单的名为 Books 的结构。在 C# 中的结构与传统的 C 或 C++ 中的结构不同。C# 中的结构有以下特点:
- 结构可带有方法、字段、索引、属性、运算符方法和事件。
- 结构可定义构造函数,但不能定义析构函数。但是,您不能为结构定义默认的构造函数。默认的构造函数是自动定义的,且不能被改变。
- 与类不同,结构不能继承其他的结构或类。
- 结构不能作为其他结构或类的基础结构。
- 结构可实现一个或多个接口。
- 结构成员不能指定为 abstract、virtual 或 protected。
- 当您使用 New 操作符创建一个结构对象时,会调用适当的构造函数来创建结构。与类不同,结构可以不使用 New 操作符即可被实例化。
- 如果不使用 New 操作符,只有在所有的字段都被初始化之后,字段才被赋值,对象才被使用。
类 vs 结构
类和结构有以下几个基本的不同点:
- 类是引用类型,结构是值类型。
- 结构不支持继承。
- 结构不能声明默认的构造函数。
- c#简单教程2
- c#简单教程1
- c#简单教程3
- mongodb c#版 官方驱动 简单教程
- c# mdi子窗体简单教程
- c# mdi子窗体简单教程
- c#教程第一课简单的欢迎程序
- 编程技巧:C# mdi子窗体简单教程
- C#简单串口调试助手【傻瓜教程】chanson_chapter01
- c#皮肤制作教程2
- C#反射教程(2)
- C#反射教程(2)
- Retrofit 2简单使用教程
- c#教程
- c#教程
- C#教程
- C#教程
- C#教程
- 对集合的一个字段排序(字符串、整型、日期)
- freemarker三目运算符
- PDF添加背景图片以及PDF文件添加页码技巧
- 看Uber产品总监的职场达人修炼之路(下)
- Android7.0以上自动更新安装apk
- c#简单教程2
- C++ fstream流的eof()函数多读一行的问题
- Myeclipse中关于js源码文件等报错问题
- 泛型类中静态成员
- [数论] 同余方程组 (hdu 3579&hdu 1573)
- DOM扩展
- Unicode 与 Unicode Transformation Format(UTF-8 / UTF-16 / UTF-32)
- QT Creator 快速入门教程 读书笔记
- 效验连续区间