下面我们继续学习C#的语法。结构struct,C#中的结构和我们PLC中建立的UDT(结构体)是一样的。里面存储了相关的不同类型的数据。

有一句话我觉得十分重要:方法是依存于结构和对象存在的。这以后我们会个更加深入的学习的。

Struct结构:

可以帮助我们一次性声明不同类型的变量。

语法:

[public] struct 结构名

{

成员;

}

如下例声明:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
public struct Person
{
public string name;
public int age;
public char gender;
}
static void Main(string[] args)
{
Person zsPerson;
zsPerson.name = "张三";
zsPerson.age = ;
zsPerson.gender = '男'; Console.ReadKey();
}
}
}

值得我们注意的是,在声明结构的时候,如果我们没加public,我们是建立不了给结构赋值的,不加public系统默认为private私有的。并且我们在命名空间之下Main之上创建的变量其实不叫变量,而是叫字段。

变量和字段的区别在于:变量可以存一个值,之后不停被覆盖,而字段类似我们PLC背景数据,可以存储若干个数值。

而且我在这要提出一个问题,我看了几个视频和数据,对于字段的命名说法不一样的,总结如下

(1)字段和变量要区别命名,例如:_Name

(2)也有反对这种命名方式的,理由是:在复杂的编程任务中,可能影响与其他语言的交互引用的作用,例如VB。net。

这在以后深入学习过程中我们在慢慢体会,也欢迎大神们给我解惑。


数组

一次性存储多个相同类型的变量。

语法:

数组的类型[] 数组名 = new 数组类型[数组长度];

数组的长度一旦固定了,就不能在被改变了。

对于int[]类型的数组,初值为0,string[]数组初值为null,bool[]数组初值为false。

下面我们介绍几种声明数组的方式

int[] nums = new int[10]; //没有声明数组元素,推荐

int[] nums = {1,2,3,4,5,6}; //隐式声明了元素和长度,推荐

int[] nums = new int[3]{1,2,3};  //不推荐,麻烦且长度和元素数量必须一致。

int[] nums = new int[]{1,2,3,4,5};  //类似第2种

下面看一个练习1:从一个整数数组中求出最大值,最小值,总和和平均值。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
static void Main(string[] args)
{
int[] nums = { ,,,,,,,,,};
int max = nums[];
int min = nums[];
int sum = ; for (int i = ; i < nums.Length; i++)
{
if (nums[i] > max)
{
max = nums[i];
} if (nums[i] < min)
{
min = nums[i];
}
sum += nums[i];
}
Console.WriteLine($"这个数组的最大值是{max},最小值是{min},总和是{sum},平均值是{sum/nums.Length}");
Console.ReadKey();
}
}
}

练习2:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
static void Main(string[] args)
{
string[] names = { "老杨","老苏","老邹","老虎","老牛","老马"};
string str = null; for (int i = ; i < names.Length-; i++)
{
str += names[i] + "|";
}
Console.WriteLine(str+names[names.Length-]);
Console.ReadKey();
}
}
}

练习3:对一个整数数组做如下处理:若元素为正数将这个元素+1,若为负数,将这个元素-1,元素为0,不变。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
static void Main(string[] args)
{
int[] nums = { ,-,,-,,,};
for (int i = ; i < nums.Length; i++)
{
if (nums[i] > )
{
nums[i] += ;
}
else if (nums[i] < )
{
nums[i] -= ;
}
else
{ }
} for (int i = ; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
Console.ReadKey();
}
}
}

练习4:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
static void Main(string[] args)
{
string[] names = { "我","是","好人"};
for (int i = ; i < names.Length/; i++)
{
string temp = names[i];
names[i] = names[names.Length - - i];
names[names.Length - - i] = temp;
}
for (int i = ; i < names.Length; i++)
{
Console.Write(names[i]);
}
Console.ReadKey();
}
}
}

练习5:冒泡排序:就是将一个数组中的元素从大到小,从小到大排列。

分析:需要两个循环,外层循环,控制比较次数,内层循环,控制交换次数。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 草稿
{
class Program
{
static void Main(string[] args)
{
int[] nums = { ,,,,,,,,,};
for (int i = ; i < nums.Length-; i++)
{
for (int j = ; j < nums.Length--i; j++)
{
if (nums[j] > nums[j+])
{
int temp = nums[j];
nums[j] = nums[j + ];
nums[j + ] = temp;
}
}
}
for (int i = ; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
Console.ReadKey();
}
}
}

这里面有一点值得我们注意,C#中的数组下标和我们PLC中数组下标正好相反,C#中数组下标的0从左面元素开始计算。

其实,这种冒泡方式的写法也就在面试的时候会用到,在我们C#中,可以直接用一个方法解决Array.Sort();(只能升序)

Array.Reverse();(反转排列)若想降序:先调用Array.Sort();后调用Array.Reverse()。

C#的结构和数组的更多相关文章

  1. C#调用C/C++动态库 封送结构体,结构体数组

    一. 结构体的传递 #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数 typedef struct { int ...

  2. [C语言入门笔记]分支结构与数组

    分支结构与数组 什么是分支结构? 分支结构是用户或者程序可以选择下一步执行哪个语句 分支结构有哪些? If If Else If Else If Switch 在初学者的学习过程中第一种和第二种比较普 ...

  3. 【C语言入门教程】7.2 结构体数组的定义和引用

    7.2 结构体数组的定义和引用 当需要使用大量的结构体变量时,可使用结构体定义数组,该数组包含与结构体相同的数据结构所组成的连续存储空间.如下例所示: struct student stu_a[50] ...

  4. Delphi结构体数组指针的问题

    //这段代码在Delphi 2007和delphi 7下是可以执行的,所以正确使用结构体数组和指针应该是这样的,已验证 unit Unit1; interface uses Windows, Mess ...

  5. C语言中的结构体,结构体数组

    C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...

  6. 结构体数组(C++)

    1.定义结构体数组 和定义结构体变量类似,定义结构体数组时只需声明其为数组即可.如: struct Student{ int num; char name[20]; char sex[5]; int ...

  7. c语言学习之基础知识点介绍(十七):写入读取结构体、数组、结构体数组

    一.结构体的写入和读取 //写入结构体 FILE *fp = fopen("/Users/ios/Desktop/1.data", "w"); if (fp) ...

  8. c语言结构体数组定义的三种方式

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; int dangdang;//成员名可以和类名同名 }ddd[];/ ...

  9. C#调用C++DLL传递结构体数组的终极解决方案

    在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...

  10. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/art ...

随机推荐

  1. python图片处理PIL

    一.PIL介绍 PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(palette).信息(info)和 ...

  2. LDAP安装

    一.介绍 LDAP 全称:Lightweight Directory Access Protocol,即“轻量级目录访问协议”. LDAP目录以树状的层次结构来存储数据.如果你对自顶向下的DNS树或U ...

  3. Keras入门(六)模型训练实时可视化

      在北京做某个项目的时候,客户要求能够对数据进行训练.预测,同时能导出模型,还有在页面上显示训练的进度.前面的几个要求都不难实现,但在页面上显示训练进度当时笔者并没有实现.   本文将会分享如何在K ...

  4. StackExchange.Redis 之 String 类型示例

    String类型很简单,就不做示例演示了,这里只贴出Helper类 /// <summary> /// 判断key是否存在 /// </summary> /// <par ...

  5. 【idea激活码】,【WebStorm激活码】,【DataGrip激活码】,【IntelliJ 全家桶系列】,【定期更新】,【第一期】

    IntelliJ IDEA.PyCharm.PhpStorm.WebStorm.RubyMide.AppCode.CLion.GoLand.DataGrip.Rider.Android Studio可 ...

  6. 【已解决】pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position 110: invalid continuation byte

    转载自勤奋的小青蛙本文链接地址: [已解决]pyinstaller UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in positi ...

  7. Go Web爬虫并发实现

    题目:Exercise: Web Crawler 直接参考了 https://github.com/golang/tour/blob/master/solutions/webcrawler.go 的实 ...

  8. (vue操作storage)Vue plugin for work with local storage,session storage and memo

    vue-ls https://www.npmjs.com/package/vue-ls NPM npm install vue-ls --save Yarn yarn add vue-ls Usage ...

  9. 企业应用开发的大趋势,65%的应用开发将通过低代码完成 ZT

    全球知名的咨询公司Gartner于近日发表了最新版的<低代码开发平台魔力象限>,并在报告中指出,到2024年65%的应用开发工作都将通过低代码的方式完成.Gartner长期关注软件开发领域 ...

  10. SpringBoot整合NoSql--(一)Redis

    简介: Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它通常被称为数据结构服务器,因为值(v ...