C# Tutorial For Beginners

C# Tutorials 自己编写的dotnet入门代码案例 阅读详情

So you just install .NET framework and want to start using C# but you don't know where to start ? well…

First you could download this complete tutorial here. There is a directory for each tutorial with a build.bat to build the tutorial and a run.bat to run the program. And every example is already compiled, just in case…

 

First tutorial

You should first open a DOS command shell. (If you don't know what it is, clic on the Start menu then run (at the bottom) and type, in the text field: "cmd".
exercise: there is an easiest way to do that, try to find it.)

You should begin to work in an empty directory for this. let call it "C:/learncs". Type in the shell:

> md C:/learncs
> cd C:/learncs
> C:

Now you should create your first C# program, type "notepad hello.cs" and type (in the notepad)

using System;

public class Hello
{
public static void Main()
{
Console.WriteLine("Hello C# World  ");
}
}

the using keyword just let you write Console at line 7, instead of System.Console. It's very usefull shortcut when you use a lot of "class" define in System.
Save the file.
Now you could compile. Type in the DOS Shell again and type:
csc /nologo /out:hello.exe hello.cs
You probaly have some errors, correct them, compile again, and now you have a working hello.exe program… type hello, see…

 

Second tutorial

Congratulation you've done the most difficult, let increase the difficulty. and create an object instance. in the DOS shell create a new directory:

> md ../learncs2
> cd ../learncs2
> notepad hello.cs

and then type, in the notepad

using System;

public class Echo
{
string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
Console.WriteLine(myString);
}
}

public class Hello
{
public static void Main()
{
Echo h = new Echo("Hello my 1st C# object !");
h.Tell();
}
}

Wouah, 25 lines! That's a program! Save it, compile it, run it…
What happened? csc look for a Main() function in your program, it should find one (and only one) and it will be the entry point of your program.
In this tutorial we create 2 classes: Echo & Hello. In the Main() method you create an Echo object (an instance of the Echo class) with the keyword new
Then we called the instance method "Tell()".
the upper case letter on class or Method is just a MS convention, do as it pleased you.
public is a visibility access, method wich are not public could not be seen from outside, there is also other visibility keywords, to learn more, clic on Start menu-> Programs -> Microsoft .NET Framework SDK -> Documentation there is a search window, an index window, etc… try to learn more about public private protected.

 

Third tutorial

Now you become to be pretty confident, I guess, so we could start using multiple file, and even a dll ? go into an other directory (or stay in this one, I won't mind) and create 2 file:

hello.cs

using System;

public class Hello
{
public static void Main()
{
HelloUtil.Echo h = new HelloUtil.Echo("Hello my 1st C# object !");
h.Tell();
}
}

echo.cs

using System;

namespace HelloUtil
{
public class Echo
{
string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
Console.WriteLine(myString);
}
}
}

Note in hello.cs I have used the syntax "HelloUtil.Echo" it's because Echo is in the namespace HelloUtil, you could have typed (at he start of the file) using HelloUtil and avoid HelloUtil., that's the way namespace work.

Now you could compile both in one .exe with
> csc /nologo /out:hello.exe *.cs
But it's not my intention, no.
Well.
(Have you tried?)
Let's go building a DLL:
> csc /nologo /t:library /out:echo.dll echo.cs
that's it (dir will confirm).
Now we could use it …
> csc /out:hello.exe /r:echo.dll hello.cs
if you typed "hello" it will worked as usual…, but if you delete "echo.dll" the program will now crash: it use the DLL. You could also change Echo.cs, rebuild the DLL and see… that's the advantage of DLL!

You could also put your DLL in the global assembly cache (GAC), and any program would be able to access it, even if the DLL is not in its directory!
to put it in the GAC, I sugest you read MS doc but here are the unexplained step:

    • create your assembly key, create it once and use it for every version. you create it with:
      sn -k myKeyName.snk
      the .snk file should be in your compilation directory (the one where your run csc)
    • create a strong asssembly title by adding in any .cs source file the following directive at top level: using System.Reflection;
      using System.Runtime.CompilerServices;
      [assembly: AssemblyTitle("My Lib Title")]
      [assembly: AssemblyVersion("1.2.3.4")]
      [assembly: AssemblyKeyFile("myKeyName.snk")]
    • now add it to the GAC:
      > gacutil.exe /if myLib.dll

       

 

By the way, did I tell you ? when I referenced the hello.dll while compiling, remember? csc /out:hello.exe /r:echo.dll hello.cs, it could have been any assembly, even a .exe !!!

 

Fourth tutorial

Congratulation you would soon be able to hack CsGL but there is one last step you should understand : interop (with C code).
You will need a C compiler, I advise gcc for windows called MinGW, it's free, it's good, it's GCC!
We will create 3 file:
echo.c

#include <stdio.h>

#define DLLOBJECT __declspec(dllexport)

DLLOBJECT void writeln(char* s)
{
printf("%s/
n", s);
}

echo.cs

using System;
using System.Runtime.InteropServices;

namespace HelloUtil
{
public class Echo
{
[DllImport("echo.native.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void writeln(string s);

string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
writeln(myString);
}
}
}

hello.cs

using System;
using HelloUtil;

public class Hello
{
public static void Main()
{
Echo h = new Echo("Hello my 1st interop code !");
h.Tell();
}
}

Hehe, here you discover a completly new thing, Attribute.
"[DllImport(.." is an attribute.
You could tag any method/field/class with any number of attribute.
They generate extra information that could be used by anyone who could understand them.
This DllImport attribute is understand by the compiler and told him that the function below is in fact in a DLL whose name is "echo.native.dll". I add a calling convention parameter as the default .NET calling convention is __stdcall whereas, in C, it's __cdecl.
By the way, if you look for DllImport in the documentation, look for DllImportAttribute, because you remove "Attribute" to attribute classname when using them, it's like this.

And now let's compile this!

> csc /nologo /t:library /out:echo.dll echo.cs
> csc /nologo /out:hello.exe /r:echo.dll hello.cs
>
> rem "if the following line don't work, read bellow.."
> gcc -shared -o echo.native.dll echo.c
> strip echo.native.dll

the 2 last line (the gcc & strip command) are for building the "C-DLL".
If they don't work maybe gcc is not in a directory listed in your path environment variable ? check with:
%lt; echo %PATH%
Well it's probably not,anyway, so type, assumin mingc is in C:/MinGW:
set PATH=C:/MinGW;%PATH%
And try again… you sure it's not a syntax error ?
If it compile test it now: hello
Great isn't it ?

Now I should admit I didn't tell you all the truth. echo.dll and echo.native.dll are not the same kind of DLL. It's not just the language (C / C#) the C one is a plain executable full of, probably, x86 instruction, whereas the C# one is what MS call a portable executable.. anyway they are different.
If you install echo.dll in the GAC it wont work because it won't find echo.native.dll except if you put in into the PATH (like C:/Windows/System32).
In the same manner when you add the reference in VS.NET echo.native.dll is overlooked and your program won't work….
So either put the native one in your path or copy it in the debug/release directory of VS.NET.
Or do everything by hand (makefile? build.bat?) and put all your dll in you build directory, and everything work fine..

 

And now..

Well, first forget about notepad. It's good to learn, but so painfull to use regularly.., try to find real developer friendly small text editor (like JFE) or a complete IDE like SharpDevelop.

Now you are ready to understand, I could tell you all the truth:
csgl.dll is a C# assembly and csgl.native.dll is just a dll compiled by GCC, upon which csgl.dll depends…

 

 

Article from: http://www.csharphelp.com/2006/12/c-tutorial-for-beginners/

Unity开发者转向UE4:核心思维转换与实战避坑指南 游戏引擎是游戏开发的核心框架,它定义了项目的架构、资源管线和工作流。从Unity转向Unreal Engine 4(UE4),不仅是工具的更换,更是开发范式的根本性重塑。Unity采用以GameObject和Component为核心的组件化思维,强调开箱即用的便捷性;而UE4则构建在Actor与Component模型之上,并深度融合了强大的可视化脚本系统——蓝图(Blueprint),提供了更高的自由度和性能天花板。这种转变要求开发者从“功能组合”思维转向“实体-组件-数据”的综合思维,并掌握C++与蓝图混 阅读详情

相关推荐

AWS EC2新手避坑指南:从网络架构到CPU积分的实操真相

EC2是AWS最基础的计算服务,本质是云上可编程虚拟机,其运行依赖VPC网络、安全组策略、实例类型资源模型与EBS存储体系四大支柱。理解EC2不能停留在‘点鼠标启动’层面,而需掌握底层原理——如VPC为何是强制前提、安全组为何是无状态入站规则、t3.micro的CPU积分如何影响实际性能与成本。这些知识直接决定部署稳定性、账单可控性及后续向容器化、Serverless演进的能力。本文聚焦真实生产场景中的高频卡点:公网访问不通、SSH连接拒绝、实例莫名扣费、磁盘空间告急等,以Ubuntu 22.04 + t3

leonard61306500的专栏 380

C#-CronTrigger Tutorial

CronTrigger Tutorial Introduction cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling c...

cxu123321的博客 246

C# Programming for Beginners: The Ultimate 7th Edition Guide

在C#中,类(Class)是一种定义了一组属性(Properties)、方法(Methods)、事件(Events)和其它组件的数据结构。它是一种引用类型,用于封装数据和功能。类是面向对象编程的基础,通过创建类的实例(Instance),也就是对象(Object),可以用来存储和操作数据。下面是一个简单的C#类定义和对象创建的例子:set;set;set;// 构造器// 创建Car类的对象在上面的代码中,定义了一个Car类,它包含了三个公共属性:MakeModel和。

weixin_42348783的博客 471

C# Tutorial and source code(非常不错的资源)

go :http://csharp.net-informations.com/

码农 803

C# Tutorial epub

C# Tutorial 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

AD20.多对多工程中将某个原理图导入PCB工程

AD20将多个原理图中的一个或多个导入到PCB文件中

鬼爷杂货铺 1万+

通过ADS协议实现与beckhoff控制器的以太网通讯

最近公司安排给我一个新huo

xulu0425的专栏 1万+

LVGL 9.4 性能调优实战:如何通过脏区合并与tile分块,让你的嵌入式UI流畅度翻倍

本文深入探讨LVGL 9.4中的性能优化技术,重点介绍脏区合并与tile分块如何显著提升嵌入式UI流畅度。通过实战案例和源码分析,详细解析局部刷新策略、显示框架调优及性能瓶颈诊断方法,帮助开发者在STM32、ESP32等资源受限设备上实现丝滑的UI体验。

weixin_28678831的博客 455

[Selenium+Java] Selenium C# Webdriver Tutorial for Beginners

Original URL:https://www.guru99.com/selenium-csharp-tutorial.html Selenium C# Webdriver Tutorial for Beginners Selenium Overview: Selenium is an open-source, webAutomation Testingtool that supp...

weixin_30246221的博客 281

U24时钟信号上升下降沿出现非单调性--简单分析

交换机U24时钟信号上升下降沿出现非单调性--简单分析

qq_45185504的博客 1136

Fastp实战指南:高效处理fastq数据的质控技巧

本文是Fastp实战指南,详细介绍了如何使用Fastp工具高效处理fastq数据的质控技巧。从数据清洗的必要性、基础命令解析,到高级参数定制、UMI数据处理及批量自动化流程,提供一站式解决方案。Fastp以其“All-in-One”的快速质控能力,帮助用户确保测序数据质量,为后续生物信息学分析奠定可靠基础。

weixin_29254029的博客 396

GDI+ Tutorial for Beginners

原文 GDI+ Tutorial for Beginners GDI+ is next evolution of GDI. Using GDI objects in earlier versions of Visual Studio was a pain. In Visual Studio .NET, Microsoft has taken care of most of the GDI pro...

weixin_33958366的博客 135

10BASE-T1S协议中的PLCA机制:实现无碰撞网络传输的关键

本文深入解析了10BASE-T1S协议中的PLCA(物理层冲突避免)机制。作为IEEE 802.3cg标准的关键部分,PLCA通过循环调度与机会传递,从根源上避免了网络碰撞,为工业与汽车等场景提供了确定性的低延迟传输保障,是实现可靠实时通信的核心技术。

weixin_29250403的博客 215

LINQ Tutorial for Beginners

LINQ LINQ is Microsoft’s technology to provide a language-level support mechanism for querying data of all types. These types include in memory arrays and collections, databases, XML documents and ...

weixin_33962923的博客 183

EtherCAT报文-BRD(广播读)抓包分析

EtherCAT报文之BRD(广播读)报文Wireshark抓包分析。

kevin1499的博客 1217

uniapp [安卓苹果App端] - 实现获取请求手机位置权限+开启定位获取经纬度/省市区地址等,检测权限手机定位是否开启并引导用户同意授权,uniApp app端调用本机开启本机定位权限及IP属地

uni-app,手机定位权限,安卓Android,苹果ios,uniapp获取本机位置权限,uni-app获取用户定位数据,uniapp APP获取用户IP属地定位,判断权限,权限被拒绝了怎么办,苹果ios系统无法开启定位,获取定位权限报错,uniapp定位获取省市区,UNI-APP如何开启手机定位功能并获取经纬度,不依赖地图第三方插件组件,打包上架后如何获取,真机运行无法获取定位,安卓和ios权限获取,判断权限检查,正式打包,详细教程,根据获取当前经纬度,再根据经纬度去定位具体省市区,自定义基座打包能成功

🏆 前端领域优质创作者 1万+

程序员必备:100本免费编程图书

编程是计算机科学中一个非常复杂且重要的任务。我们在计算机系统、移动手机或其他地方所享受的,都是程序员通过无尽的努力开发出来的。编程分两种类型:机器水平的和高级编程。大多数程序员都依赖于高级编程。编程就是利用一些预定义的规则和资源,通过重复不断的使用,为社会或客户开发出有创造性的、新颖的、实用的以及美好的东西。你需要做的就是学习需要遵循的语法和规则,然后利用这些以及你的创造性来进行...

weixin_30548917的博客 145

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

SLAM for Dummies SLAM初学者教程 中文翻译 1到4章

SLAM for Dummies SLAM初学者教程A Tutorial Approach to Simultaneous Localization and Mapping 一本关于实时定位及绘图(SLAM)的入门指导教程目录 1. 目录 2. 简介 3. 关于SLAM 4. 硬件 -机器人 -距离量测设备 5. SLAM处理过程 6. 雷达数据 7. 里程计...

weixin_34406061的博客 250

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

说说C#的async和await

C# 5.0中引入了async 和 await。这两个关键字可以让你更方便的写出异步代码。 看个例子: public class MyClass { public MyClass() { DisplayValue(); //这里不会阻塞 System.Diagnostics.Debug.WriteLine("MyClass() End."); } public Task G

许耀驰的博客 18万+

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

C#_Serilog Tutorial

一、安装 官方网站:https://serilog.net/,或在Nuget中直接搜索Serilog下载。 二、使用 static void Main(string[] args) { Logger log = new LoggerConfiguration().CreateLogger(); log.Debug("Hello World"); } 使用LoggerConfiguration对象的CreateL

目标:MVP 1081

C# Tutorial - Simple Threaded TCP Server

In this tutorial Im going to show you how to build a threaded tcp server with C#. If youve ever worked with Windows sockets, you know how difficult this can sometimes be. However, thanks to the .N

懒虫不懒,不信你看! 2056

【C#】Microsoft learn入门C#教程简介

catalogue。

hmywillstronger的博客 1226

滑动窗口_LeetCode_30.串联所有单词的子串:一次 159/183 的 WA 复盘

本文复盘LeetCode 30题“串联所有单词的子串”的解法演进。核心思想是:按起点对len取模分组,每组内滑动窗口。第一版因hash2未重置导致奇数起点漏解,修复后通过分组+滑窗优化,实现正确性与效率平衡。关键点在于“边界对齐”与“组内独立滑动”,最终代码通过183/183测试用例,复杂度降至$O(n \cdot m \cdot len)$,并总结出可复用模板。

2501_93610107的博客 760
上一篇: static 变量和static 构造函数 以及对象初始化顺序
下一篇: C#中对注册表的操作
imbiz
博客等级 码龄17年 11粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值