找到 34423 篇文章 关于编程

编写一个 C# 程序进行基本的算术运算

Samual Sam
更新于 2020年6月22日 07:23:17

505 次浏览

让我们进行以下算术运算 -序号运算符和描述1+将两个操作数相加2-从第一个操作数中减去第二个操作数3*将两个操作数相乘4/将分子除以分母以下是一个使用上述运算符执行算术运算的示例 -示例 在线演示using System; namespace OperatorsApplication {    class Program {       static void Main(string[] args) {          int a = 40;          int b = 20;          int c;          c = a + b;          Console.WriteLine("加法: {0}", c);          c = a - b;          Console.WriteLine("减法: {0}", c);                c = a * b;          Console.WriteLine("乘法: {0}", c);          c = a / b;          Console.WriteLine("除法: {0}", c);          Console.ReadLine();       }    } }输出加法: 60 减法: 20 乘法: 800 除法: 2

编写一个 C# 程序来查找最大公约数 (GCD) 和最小公倍数 (LCM)?

Ankith Reddy
更新于 2020年6月22日 07:24:03

949 次浏览

最大公约数 (GCD)最大公约数是整除每个整数的最大正整数。最小公倍数 (LCM)两个数的最小公倍数是可以被这两个数整除的最小整数。以下是如何计算最大公约数和最小公倍数的示例。在这里,我们计算 10 和 16 的最小公倍数和最大公约数 -示例 在线演示using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Program {       static void Main(string[] args) {          int val1, val2, n1, n2, x;          int resLCM, resGCD;          val1 ... 阅读更多

C# 中 String.Copy() 和 String.Clone() 方法的区别是什么?

George John
更新于 2020年6月21日 16:55:18

436 次浏览

String.Copy() 方法创建一个新的 String 实例。这与指定的 String 相同。以下是 Copy() 方法的示例 -示例 在线演示using System; class Demo {    static void Main(String[] args) {       string str1 = "mark";       string str2 = "marcus";       Console.WriteLine("str1 = '{0}'", str1);       Console.WriteLine("str2 = '{0}'", str2);       Console.WriteLine("使用 String.Copy() 之后...");       str2 = String.Copy(str1);       Console.WriteLine("str1 = '{0}'", str1);       Console.WriteLine("str2 = '{0}'", str2);    } }输出str1 = 'mark' str2 = ... 阅读更多

C# 中接口和类的区别是什么?

karthikeya Boyini
更新于 2020年6月21日 16:54:30

1K+ 次浏览

接口是一个没有字段或方法实现的类。它不能实现它定义的方法。类通常实现接口中定义的方法。接口接口定义属性、方法和事件,这些是接口的成员。接口只包含成员的声明。派生类有责任定义成员。public interface interface_name {    // interface_members }类类是数据类型的蓝图。它实际上并不定义任何数据,但它确实定义了类名的含义。也就是说,类的对象包含什么以及... 阅读更多

C# 中字面量和常量的区别是什么?

Chandu yadav
更新于 2020年6月21日 16:55:46

444 次浏览

常量是指程序在其执行期间可能不会更改的固定值。这些固定值也称为字面量。常量可以是任何基本数据类型,例如整数常量、浮点常量、字符常量或字符串字面量。还有枚举常量。整数字面量可以是十进制或十六进制常量。前缀指定基数或基数:十六进制为 0x 或 0X,十进制没有前缀 id。150 300u浮点字面量具有整数部分、小数点、小数部分和指数部分。3.14159 235468E-7F字符串 ... 阅读更多

C# 中 String 和 string 的区别是什么?

Samual Sam
更新于 2020年6月21日 16:56:33

456 次浏览

String 代表 System.String,而 string 是 C# 中 System.String 的别名 -例如string str = "Welcome!";这并非必需,但通常在使用类时使用 String。string str = String.Format("Welcome! {0}!", user);由于 string 是 System.String 的别名。其他数据类型的别名为 -示例object: System.Object string: System.String bool: System.Boolean float: System.Single double: System.Double decimal: System.Decimal byte: System.Byte sbyte: System.SByte short: System.Int16 ushort: System.UInt16 int: System.Int32 uint: System.UInt32 long: System.Int64 ulong: System.UInt64 char: System.Char

如何使用 C# 将集合复制到数组?

Ankith Reddy
更新于 2020年6月21日 16:42:08

142 次浏览

要将集合复制到数组,首先设置它 -List < string > list1 = new List < string > (); list1.Add("Car"); list1.Add("Bus"); list1.Add("Motorbike"); list1.Add("Train");现在声明一个字符串数组并使用 CopyTo() 方法进行复制 -string[] arr = new string[20]; list1.CopyTo(arr);让我们看看将集合复制到数组的完整代码 -示例using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("Car");       list1.Add("Bus");       list1.Add("Motobike");       ... 阅读更多

C# 中 StringWriter 与 StringReader?

George John
更新于 2020年6月21日 16:44:42

293 次浏览

StringReader 和 StringWriter 派生自 TextReader 和 TextWriterStringWriter 用于写入字符串缓冲区。它实现了一个 TextWriter,用于将信息写入字符串。对于 StringWriter -示例StringWriter sWriter = new StringWriter(); while(true) {    myChar = strReader.Read();    if(myChar == -1) break;    convertedChar = Convert.ToChar(myChar);    if(convertedChar == '.') {       strWriter.Write(".");       sReader.Read();       sReader.Read();    }else {       sWriter.Write(convertedChar);    } } }StringReader 用于读取字符串 -示例StringBuilder sbuilder = new StringBuilder(); // 追加 sbuilder.AppendLine("Line one characters"); sbuilder.AppendLine("Line two characters"); sbuilder.AppendLine("Line three characters"); // ... 阅读更多

C# 中的虚函数是什么?

Samual Sam
更新于 2020年6月21日 16:51:25

4K+ 次浏览

virtual 关键字用于修改方法、属性、索引器或事件。当您在类中定义一个要在继承类中实现的函数时,可以使用虚函数。虚函数可以在不同的继承类中以不同的方式实现,并且对这些函数的调用将在运行时确定。以下是一个虚函数public virtual int area() { }这是一个显示如何使用虚函数的示例 -示例using System; namespace PolymorphismApplication {    class Shape {       protected int width, height;           ... 阅读更多

什么是 C# 中的非静态类?

Chandu yadav
更新于 2020年6月21日 16:51:45

1K+ 次浏览

可以实例化非静态类,而不能实例化静态类,即不能使用 new 关键字创建类类型的变量。非静态类可以具有实例方法和静态方法。通过使用类名本身访问静态类的成员,而静态类是密封的。非静态类的示例 -public class Calculate静态类的示例 -public static class Calculate

广告
© . All rights reserved.