swig.js {# #}_在JS背景下了解C#的基础知识

swig.js {# #}

One of the common questions I always find myself asking an SWE is, “how do you know so many languages?” The response is always mixed with a joke or two. After spending time building full-stack applications using JavaScript with Ruby on Rails backend, I wanted to explore another backend framework that I am always seeing on job descriptions. .Net is a common framework that companies still use today. Until recently with JS and React, .net is normally connected with C#. So I wanted to dive deeper into the language and what difference/similarity does it have with JavaScript.

我经常发现自己会问一个SWE的常见问题之一是:“你怎么知道那么多语言?” 响应总是混杂着一两个笑话。 在花了一些时间在Ruby on Rails后端上使用JavaScript构建全栈应用程序之后,我想探索另一个在职位描述中经常看到的后端框架。 .Net是公司今天仍然使用的通用框架。 直到最近使用JS和React,.net才通常与C#连接。 因此,我想更深入地研究该语言以及它与JavaScript有什么区别/相似之处。

打印到CMD (Print to the CMD)

Let’s start with printing out an output to the CMD. In JavaScript, we can write our function and use console.log() to get our output. In C# we write Console.WriteLine(“Hello World”); to execute the output. However, C# will terminate the program once the line is executed. In order to keep the window up, we write another line of code: Console.ReadLine(); This will now leave the window open until you click Enter. This tells the program that we are done.

让我们开始将输出打印到CMD。 在JavaScript中,我们可以编写函数并使用console.log()获取输出。 在C#中,我们编写Console.WriteLine(“ Hello World”); 执行输出。 但是,一旦执行该行,C#将终止程序。 为了保持窗口状态,我们编写了另一行代码:Console.ReadLine(); 现在,这将使窗口保持打开状态,直到您单击Enter。 这告诉程序我们已经完成。

Please note: You need to include ‘;’ at the end of each line in C# so it can execute.

请注意:您需要添加“;” 在C#中每行的末尾,因此它可以执行。

变数 (Variables)

Javascript we know we need to declare any data type using ‘var’, ‘const’, and ‘let’. Today, we taught to avoid using var since const and let came out with ES6. Let’s use a classmate's name and their age and declare the variable. In JS, we can use let for the age of the classmate. We use ‘let’ because we can update the age as our program progress in time. We can use ‘const’ for the name because a person's name is generally not changed. In C#, use the word of the data type. For example, we use string to declare the variable name of ‘classmateName’ and int, for number, to declare ‘classmateAge’. With great powers, comes great responsibilities. Now that we know how to declare variables for name and age, we can use these variables to interpolate in our program.

我们知道需要使用'var','const'和'let'声明任何数据类型的Javascript。 今天,我们教从const开始避免使用var,而是让ES6诞生了。 让我们使用一个同学的名字和他们的年龄,并声明变量。 在JS中,我们可以使用let作为同学的年龄。 我们使用'let'是因为我们可以随着计划的进展及时更新年龄。 我们可以使用“ const”作为名字,因为一个人的名字通常不会改变。 在C#中,使用数据类型的单词。 例如,我们使用字符串声明变量名称“ classmateName”,并使用int表示数字,声明“ classmateAge”。 权力越大,责任就越大。 现在我们知道了如何声明名称和年龄的变量,我们可以使用这些变量在程序中进行内插。

Image for post
Assigning variable
分配变量

Interpolation

插补

Just like JS, string interpolation is very similar to implement in C#. We use the concatenation method to use the ‘+’ symbol.

就像JS一样,字符串插值与C#中的实现非常相似。 我们使用串联方法来使用'+'符号。

Image for post

资料类型 (Data Types)

To store plain text, as shown above, we can use string to declare with the variable. C# also allows us to use single characters in our program. This is executed by using ‘char’ Just like string and int it is followed by the variable name char grade = ‘A’; Note: char can only accept one character. Anything more, you will need to use string. With integer, C# gives us 4 different ways to declare a number. ‘Int’ is the more generic to use for whole numbers and C# is smart enough to allow us to use negative numbers as well. If we have a number that’s a decimal point, we can declare by using ‘float’, ‘double’, or ‘decimal’. When using anything other than ‘int’, you need to include a decimal point. 3.4 or 3.0. Each has its own limitations and you can read more using the docs here, https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types

如上所示,要存储纯文本,我们可以使用字符串使用变量进行声明。 C#还允许我们在程序中使用单个字符。 这是通过使用'char'执行的,就像字符串和int一样,其后是变量名称char grade ='A'; 注意:char只能接受一个字符。 除此之外,您将需要使用字符串。 使用整数,C#为我们提供了4种不同的方法来声明数字。 'Int'是更通用的整数,C#足够聪明,可以让我们也使用负数。 如果我们的数字是小数点,则可以使用“ float”,“ double”或“ decimal”进行声明。 当使用除“ int”以外的任何东西时,您需要包括一个小数点。 3.4或3.0。 每个都有其自身的局限性,您可以使用此处的文档https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types阅读更多内容

Boolean is a variable that is either true or false. We see this in JS as well and can understand how it works in C#. For the most part, the data types above, also shown below, are the core data types that we use and can relate to in C#.

布尔值是true或false的变量。 我们也在JS中看到了这一点,并且可以了解它在C#中的工作方式。 在大多数情况下,上面的数据类型(也如下所示)是我们在C#中使用并可以与之关联的核心数据类型。

Image for post
C# DataTypes
C#数据类型

了解C#中的方法 (Understanding Methods in C#)

Methods in C# is also similar to the ones in JS. For example, to change a given string to all uppercase, you can use .toUpperCase() to print everything in uppercase. Similarily in C#, you would use ToUpper(); or ToLower(); for lowercase.

C#中的方法也类似于JS中的方法。 例如,要将给定的字符串更改为全部大写,可以使用.toUpperCase()以大写形式打印所有内容。 类似地,在C#中,您将使用ToUpper();。 或ToLower(); 小写字母。

结论 (Conclusion)

To answer my question before my knowledge of programming, OOP language are somewhat similar to each other. Understanding lines of code or concept for a new language can be intimidated but with practice, it’s doable. To my surprise, both JS and C# are similar when it comes to a basic understanding of each. Being able to start a new language such as C# showed me that we should not have a fear to embrace a new language. It is fun making a comparison between the two and understands the differences and similarities they share.

为了在我没有编程知识之前就回答我的问题,OOP语言有些相似。 理解一种新语言的代码行或概念行可能会受到威胁,但是通过实践,这是可行的。 令我惊讶的是,当对JS和C#进行基本了解时,它们都是相似的。 能够启动一种新的语言(例如C#)向我表明,我们不必担心会接受一种新语言。 在两者之间进行比较很有趣,并且了解它们之间的异同。

翻译自: https://medium.com/swlh/understanding-the-fundamentals-of-c-with-a-js-background-f416b436d5f

swig.js {# #}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值