static int FindMax(int number1, int number2)
{
int result;
if (number1 >= number2)
{
result = number1;
}
else
{
result = number2;
}
return result;
}
Console.WriteLine("请输入第一个数字:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个数字:");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("两个数字中最大的是:");
Console.WriteLine(FindMax(a, b));
static int FindMax(int number1, int number2) => number1 >= number2 ? number1 : number2;
Console.WriteLine("请输入第一个数字:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个数字:");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("两个数字中最大的是:");
Console.WriteLine(FindMax(a, b));
using System;
using System.Collections.Generic;
using System.Text;
namespace projAboveAvg
{
class Program
{
static void Main(string[] args)
{
int mean = 0;
int [] scores = new int[]
{
90,65,88,70,46,81,100,68
};
string [] names = new string[]
{
"景珍","林惠洋","成蓉","洪南昌","龙玉民","单江开","田武山","王三明"
};
foreach(int x in scores)
{
mean = mean + x;
}
mean = mean / scores.Length;
Console.WriteLine("平均分是{0},高于平均分的有:",mean);
for (int i=0; i<scores.Length;i++)
{
if (scores[i]>=mean)
{
Console.Write("{0} ",names[i]);
}
}
}
}
}
// See https://aka.ms/new-console-template for more information
using System.Collections;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
Console.WriteLine(list[1]);
}
}
}
// See https://aka.ms/new-console-template for more information
using HelloWorld;
namespace HelloWorld
{
public class Staff
{
public Staff()
{
Console.WriteLine("员工初始化");
}
public int Number { get; set; }
public Staff(int number)
{
this.Number = number;
}
}
public class Manager :Staff
{
public Manager()
{
Console.WriteLine("经理初始化");
}
public Manager(int number) : base(number)
{
Console.WriteLine($"{number}经理初始化");
}
}
class Problem
{
static void Main(string[] args) {
var manager = new Manager(123);
}
}
}
// See https://aka.ms/new-console-template for more information
namespace Helloworld
{
class Logger
{
public void Log(string message) {
Console.WriteLine($"日志:{DateTime.Now} - {message}");
}
}
}
安装文件:Install.cs
// See https://aka.ms/new-console-template for more information
namespace Helloworld
{
class Install
{
private readonly Logger logger;
public Install(Logger logger)
{
this.logger = logger;
}
public void install()
{
logger.Log("安装开始");
}
}
}
数据传输文件:DbMigrator.cs
// See https://aka.ms/new-console-template for more information
namespace Helloworld
{
class DbMigrator
{
private readonly Logger _logger;
public DbMigrator(Logger logger)
{
_logger = logger;
}
public void Migrate()
{
_logger.Log("数据迁移开始");
}
}
}
主文件:Program.cs
// See https://aka.ms/new-console-template for more information
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
var logger = new Logger();
var dbMigrate = new DbMigrator(logger);
var install = new Install(logger);
install.install();
dbMigrate.Migrate();
Console.WriteLine();
}
}
}
string[] words = new string[]
{
"The",
"quick",
"brown",
"fox"
};
Console.WriteLine(words[^1]);
var list = words[0..3];
for (int i = 0;i <list.Length;i++)
{
Console.WriteLine(list[i]);
}
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
string name = "Zoc";
int age = 36;
string message = "My name is {0}. I'm {1}.";
string output = string.Format(message,name,age);
Console.WriteLine(output);
string message2 = $"My name is {name}. \nI'm {age}.";
Console.WriteLine(message2);
// 原意字符串输出
string message3 = @"My name is {name}.
I'm {age}.";
Console.WriteLine(message3);
string message4 = @"My name is {0}. I'm {1}.";
string output2 = string.Format(message4,name,age);
Console.WriteLine(output2);
Programs typically use data to perform tasks. Creating a variable reserves a memory location, or a space in memory, for storing values. It is called variable because the information stored in that location can be changed when the program is running.
To use a variable, it must first be declared by specifying the name and data type.
A variable name, also called an identifier, can contain letters, numbers and the underscore character (_) and must start with a letter or underscore.
Although the name of a variable can be any set of letters and numbers, the best identifier is descriptive of the data it will contain. this is very important in order to create clear, understandable and readable code.
A data type defines the information that can be stored in a variable, the size of needed memory and the operations that can be performed with the variable.
For example, to store an integer value (a whole number) in a vairable, use the int keyword:
int myAge;
myAge = 18;
int myAge = 18;
There are a number of built-in data types in C#. The most common are:
int
float
double
char
bool
string
The statements below use C# data types:
int x = 24;
double pi = 3.14;
char y = "a";
bool isTure = true;
string myName ="Ao";
C# is one of the most popular modern programming languages. C# is elegant and powerful. You can use it to create video games, web, mobile, database applications, and much, much more.
Humans use computer programs to communicate with machines. Without computer programs, we wouldn’t have smartphones, websites, or even exploration in outer space.
Learning some coding can help you innovate and create different solutions to problems, giving you a competitive edge in this technology-driven world.
Most computer programs are designed to produce outputs. The simplest output consists of a message displayed on the screen. Examples of outputs are:
“A new message” notifications
“Game over” is displayed on the screen when playing video games
Your account balance when checking your online banking app.
Coders use outputs all the time to check that the computer is following the given instructions and fix problems with code. The following line of code displays a message on the screen as an output:
Console.WriteLine("Hello world of C#!");
Real computer programs can include thousands of lines of code. A line of code is called a statement. A statement performs a specific task.
C# is an elegant object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework.
The .NET framework consists of the Common Language Runtime (CLR) and the .NET Framework class library.
The CLR is the foundation of the .NET Framework. It manages code at execution time, code accuracy, and many other aspects of your code.
The class library is a collection of classes, interfaces, and value types that enable you to accomplish a range of common programming tasks, such as data collection, file access, and working with text.
C# programs use the .NET Framework class library extensively to do common tasks and provide various functionalities.