網站首頁 編程語言 正文
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
相關推薦
- 2022-04-12 python關閉print輸出信息詳情_python
- 2022-04-09 Windows 環境配置Github 的SSH key
- 2023-10-31 springboot 線程池參數解釋
- 2023-02-02 C語言中的直接插入排序(帶圖詳細)_C 語言
- 2022-11-22 Python?flask框架定時任務apscheduler應用介紹_python
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 2022-07-19 react Context的基本使用( react跨組件傳遞數據 )
- 2022-03-30 C#數據類型轉換(顯式轉型、隱式轉型、強制轉型)_C#教程
- 最近更新
-
- 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同步修改后的遠程分支