網站首頁 編程語言 正文
Top-level statements must precede namespace and type declarations. [Test]csharp(CS8803)
作者:Morris_ 更新時間: 2022-04-12 編程語言C#
.NET 6
結構體
C# 控制臺應用程序定義了一個結構體,然后創建結構體實例,報如下錯誤:
Top-level statements must precede namespace and type declarations. [Test]csharp(CS8803)
把 Student stu1 = new Student(“小明”,22); 結構體的定義放在初始化的后面就不報錯了,這是為什么?
我個人的理解是因為C#控制臺應用程序,從上往下執行,執行到 Student stu1 = new Student(“小明”,22); 然后再去找 Student 的定義以及它的構造函數。
我猜,這個問題在其他的應用程序中應該不會存在,只是出現在控制臺應用程序這種從上往下有嚴格執行順序的應用程序中。
- C# 結構體
- 結構體類型數組
代碼如下:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Student stu1 = new Student("小明",22);
Student stu2 = new Student("小紅",23);
Student stu3 = new Student("小麗",20);
Student stu4 = new Student("小王",21);
Student [] array = new Student[4]; // 初始化數組 類型 [] 數組名 = new 類型[容量]
array[0] = stu1;
array[1] = stu2;
array[2] = stu3;
array[3] = stu4;
foreach (Student stu in array) { // 遍歷數組 foreach(類型 變量名 in 數組名)
Console.WriteLine("{0}的年齡是{1}",stu.name, stu.age); // 打印輸出姓名和年齡
}
public struct Student { // 定義結構體Student
public string name;
public int age;
public Student(string name, int age) { // 構造函數
this.name = name;
this.age = age;
}
public void PrintStudent() {
Console.WriteLine(this.name,this.age);
}
}
原文鏈接:https://blog.csdn.net/Morris_/article/details/123471477
相關推薦
- 2023-04-02 linux?top命令基本實戰_linux shell
- 2023-06-18 聚星C#數字信號處理工具包頻譜分析的用法_C#教程
- 2024-01-16 where語句后跟case語句
- 2022-01-15 Es6中用Set去重
- 2023-05-22 python常用時間庫time、datetime與時間格式之間的轉換教程_python
- 2022-07-28 Redis特殊數據類型Geospatial地理空間_Redis
- 2022-08-04 Python中reduce函數詳解_python
- 2022-12-04 React18?useState何時執行更新及微任務理解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支