mysql语句中变量 c#_C#基础知识-您的第一个C#程序,类型和变量以及流控制语句...

 2023-09-06 阅读 23 评论 0

摘要:mysql语句中变量 c# 建立 (Setup) LinqPad is an .NET scratchpad to quickly test your C# code snippets. The standard edition is free and a perfect tool for beginners to execute language statements, expressions and programs. LinqPad是一个.NET暂存器,可快

mysql语句中变量 c#

建立 (Setup)

LinqPad is an .NET scratchpad to quickly test your C# code snippets. The standard edition is free and a perfect tool for beginners to execute language statements, expressions and programs.

LinqPad是一个.NET暂存器,可快速测试您的C#代码段。 标准版是免费的,是初学者执行语言语句,表达式和程序的理想工具。

Alternatively, you could also download Visual Studio Community 2015 which is an extensible IDE used by most professionals for creating enterprise applications.

或者,您也可以下载Visual Studio Community 2015 ,它是大多数专业人士用来创建企业应用程序的可扩展IDE 。

您的第一个C#程序 (Your First C# Program)

//this is the single line comment/** This is multiline comment,
compiler ignores any code inside comment blocks.
**///This is the namespace, part of the standard .NET Framework Class Library
using System;
// namespace defines the scope of related objects into packages
namespace Learning.CSharp
{  // name of the class, should be same as of .cs filepublic class Program{//entry point method for console applicationspublic static void Main(){//print lines on consoleConsole.WriteLine("Hello, World!");//Reads the next line of characters from the standard input stream.Most common use is to pause program execution before clearing the console.Console.ReadLine();}}
}

Every C# console application must have a Main method which is the entry point of the program.

每个C#控制台应用程序都必须具有Main方法 , 该方法是程序的入口点。

Edit HelloWorld in .NET Fiddle, a tool inspired by JSFiddle where you can alter the code snippets and check the output for yourself. Note, this is just to share and test the code snippets, not to be used for developing applications.

在.NET Fiddle中编辑HelloWorld ,这是受JSFiddle启发的工具,您可以在其中更改代码段并自行检查输出。 注意,这仅仅是共享和测试代码片段,而不用于开发应用程序。

If you are using visual Studio, follow this tutorial to create console application and understand your first C# program.

如果您使用的是Visual Studio,请按照本教程创建控制台应用程序并了解您的第一个C#程序。

类型和变量 (Types and Variables)

C# is a strongly typed language. Every variable has a type. Every expression or statement evaluates to a value. There are two kinds of types in C#:

C#是一种强类型语言。 每个变量都有一个类型。 每个表达式或语句均求值。 C#有两种类型:

  • Value types

    值类型
  • Reference types.

    参考类型。

Value Types : Variables that are value types directly contain values. Assigning one value type variable to another copies the contained value.

值类型 :作为值类型的变量直接包含值。 将一个值类型变量分配给另一个将复制包含的值。

Edit in .NET Fiddle

在.NET Fiddle中编辑

int a = 10;
int b = 20;
a=b;
Console.WriteLine(a); //prints 20
Console.WriteLine(b); //prints 20

Note that in other dynamic languages this could be different, but in C# this is always a value copy. When value type is created, a single space most likely in stack is created, which is a “LIFO” (last in, first out) data structure. The stack has size limits and memory operations are efficient. Few examples of built-in data types are int, float, double, decimal, char and string.

请注意,在其他动态语言中,这可能有所不同,但是在C#中,这始终是值副本。 创建值类型时,将创建最有可能在堆栈中的单个空间,即“ LIFO”(后进先出)数据结构。 堆栈具有大小限制,并且内存操作高效。 内置数据类型的几个示例是int, float, double, decimal, char and string

TypeExampleDescription
Integerint fooInt = 7;Signed 32-bit Integer
Longlong fooLong = 3000L;Signed 64-bit integer.L is used to specify that this variable value is of type long/ulong
Doubledouble fooDouble = 20.99;Precision: 15-16 digits
Floatfloat fooFloat = 314.5f;Precision: 7 digits.F is used to specify that this variable value is of type float
Decimaldecimal fooDecimal = 23.3m;Precision: 28-29 digits.Its more precision and smaller range makes it appropriate for financial and monetary calculations
Charchar fooChar = 'Z';A single 16-bit Unicode character
Booleanbool fooBoolean = false;Boolean - true & false
Stringstring fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs);A string of Unicode characters.
类型 描述
整数 int fooInt = 7; 签名的32位整数
long fooLong = 3000L; 有符号的64位整数。 L用于指定此变量值的类型为long / ulong
double fooDouble = 20.99; 精度: 15-16位
浮动 float fooFloat = 314.5f; 精度: 7位数F用于指定此变量值的类型为float
小数 decimal fooDecimal = 23.3m; 精度: 28-29位数字。它的精度更高,范围更小,适合进行财务和货币计算
烧焦 char fooChar = 'Z'; 单个16位Unicode字符
布尔型 bool fooBoolean = false; 布尔值- 对与错
string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs); 一串Unicode字符。

For complete list of all built-in data types see here

有关所有内置数据类型的完整列表,请参见此处

Reference types : Variables of reference types store references to their objects, which means they store the address to the location of data on the stack, also known as pointers. Actual data is stored on the heap memory. Assigning reference type to another doesn’t copy the data, instead it creates the second copy of reference which points to the same location on the heap.

引用类型引用类型的变量存储对其对象的引用,这意味着它们将地址存储到堆栈上数据的位置,也称为指针。 实际数据存储在堆内存中。 将引用类型分配给另一个引用类型不会复制数据,而是会创建引用的第二个副本,该副本指向堆上的相同位置。

In heap, objects are allocated and deallocated in random order that is why this requires the overhead of memory management and garbage collection.aspx).

在堆中,对象是以随机顺序分配和释放的,这就是为什么这需要内存管理和垃圾回收( .aspx)开销的原因。

Unless you are writing unsafe code or dealing with unmanaged code.aspx), you don’t need to worry about the lifetime of your memory locations. .NET compiler and CLR will take care of this, but it’s still good to keep this mind in order to optimize performance of your applications.

除非您正在编写不安全的代码或处理非托管代码( .aspx),否则您不必担心内存位置的生命周期。 .NET编译器和CLR将解决此问题,但是仍然要记住此思想以便优化应用程序的性能。

More information here.

更多信息在这里 。

流程控制声明 (Flow Control Statements)

如果另有陈述 (If else statement)

int myScore = 700;
if (myScore == 700) {Console.WriteLine("I get printed on the console");
} else if (myScore > 10) {Console.WriteLine("I don't");
} else {Console.WriteLine("I also don't");
}/** Ternary operatorsA simple if/else can also be written as follows<condition> ? <true> : <false> **/
int myNumber = 10;
string isTrue = myNumber == 10 ? "Yes" : "No";

Edit in .NET Fiddle

在.NET Fiddle中编辑

切换语句 (Switch statement)

using System;
public class Program {public static void Main() {int myNumber = 0;switch (myNumber) { // A switch section can have more than one case label. case 0:case 1:{Console.WriteLine(“Case 0 or 1”);break;}}

Edit in .NET Fiddle

在.NET Fiddle中编辑

对于与FOREACH (For & Foreach)

for (int i = 0; i < 10; i++) {Console.WriteLine(i); //prints 0-9 }Console.WriteLine(Environment.NewLine);for (int i = 0; i <= 10; i++) {Console.WriteLine(i); //prints 0-10 }Console.WriteLine(Environment.NewLine);for (int i = 10 - 1; i >= 0; i—) //decrement loop {Console.WriteLine(i); //prints 9-0 }Console.WriteLine(Environment.NewLine); //for (; ; ) { // All of the expressions are optional. This statement //creates an infinite loop.* //}

Edit in .NET Fiddle

在.NET Fiddle中编辑

边 做边做 (While & do-while )

// Continue the while-loop until index is equal to 10. 
int i = 0;
while (i < 10) {Console.Write(“While statement”);Console.WriteLine(i); // Write the index to the screen. i++;// Increment the variable. }int number = 0; // do work first, until condition is satisfied i.e Terminates when number equals 4. do {Console.WriteLine(number); //prints the value from 0-4 number++; // Add one to number. }while (number <= 4);

Edit in .NET Fiddle

在.NET Fiddle中编辑

翻译自: https://www.freecodecamp.org/news/c-basics-your-first-c-program-types-and-variables-and-flow-control-statements/

mysql语句中变量 c#

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/7131.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息