Contents:
C# (pronounced as C Sharp), developed by Microsoft, uses the .NET framework. It’s a multi-purpose programming language that is modern and object-oriented.
C# is more of a blend of C and C++ programming languages. However, in contrast, the syntax of the C# language is expressive, simple, intuitive, and easy to understand.
Complexities introduced by the C++ language are highly simplified by the C# language. This includes enumerations, nullable types, direct memory access, delegates, etc., which are also not found in Java.
C# is ranked as the 7th most popular programming language. While most of the programming languages are created to carry out a specific set of tasks, C# is a general-purpose programming language.
You can use C# to work with databases, create high-speed graphics for video games, play audio/video effects, control connected devices, and more.
C# is interpreted as well as compiled. Unlike C++ and Java, C# provides excellent screen handling support for Web as well as Desktops. This is where C# stands with respect to other popular programming languages.
There are a number of ways to do C# Programming
1) .NET CLI and your choice of text or code editor.
2) .NET CLI and Visual Studio Code (install the .NET SDK and Visual Studio Code).
4) Visual Studio alternative IDE such as MonoDevelop or SharpDevelop.
5) C# online code editor. This is the simplest approach to quickly learn C# Programming.
using System; namespace HelloWorld { public class MyHello { public static void Main() { Console.WriteLine("Hello World"); } } }online code sample at IdeOne
Meaning of codes
Line 1: using System means that we can use classes from the System namespace.
Line 2: A blank line. C# ignores white space. However, multiple lines makes the code more readable.
Line 3: namespace is used to organize your code, and it is a container for classes and other namespaces.
Line 4: The curly braces {} marks the beginning and the end of a block of code.
Line 5: class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class MyHello.
Line 7: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets {} will be executed.
Line 9: Console is a class of the System namespace, which has a WriteLine() method that is used to output/print text. In our example it will output "Hello World!".
Keywords and Identifiers
Keywords are predefined sets of reserved words that have special meaning in a program. The meaning of keywords can not be changed, neither can they be directly used as identifiers in a program.
Identifiers are the name given to entities such as variables, methods, classes, etc. They are tokens in a program which uniquely identify an element.
Keywords | Identifiers |
---|---|
using | System |
namespace | HelloWorld (namespace) |
class | Hello (class) |
static | Main (method) |
void | args |
string | Console |
WriteLine |
NOTE:The "Hello World!" inside WriteLine
method is a string literal.
REFERENCE:https://www.programiz.com/csharp-programming/keywords-identifiers
Program design tools are used as a plan for developing and communicating programming codes.
1) PSEUDOCODE
1. BEGIN
2. PRINT "Hello World"
3. END
Online coding: https://www.jdoodle.com/compile-c-sharp-online/
Processing Integer Data
using System; namespace HelloNumber { public class MyNumber { public static void Main(string[] args) { //declare int number1=0; //input Console.WriteLine("Enter value for number1:"); number1 = Convert.ToInt32( Console.ReadLine() ); //process int result= number1 * 2; //output Console.WriteLine("number1 x 2: " + result ); } } }online code sample at JDoodle
Processing Float Data
using System; namespace HelloFloat { public class MyFloat { public static void Main(string[] args) { //declare float float1=0; //input Console.WriteLine("Enter value for float1:"); float1 = Convert.ToSingle( Console.ReadLine() ); //process float result= float1 / 2; //output Console.WriteLine("float1 / 2: " + result ); } } }online code sample at JDoodle
TRY: use Double and Decimal data type with Convert.ToDecimal() and Convert.ToDouble() respectively.
Processing Char Data
using System; namespace HelloChar { public class MyChar { public static void Main(string[] args) { //declare char char1='A'; //input Console.WriteLine("Enter value for char1:"); char1 = Console.ReadLine()[0]; //process bool result= Char.IsLetter(char1); //output Console.WriteLine("char1 is a letter: " + result ); } } }online code sample at JDoodle
Processing String Data
using System; namespace HelloString { public class MyString { public static void Main(string[] args) { //declare string string1=""; //input Console.WriteLine("Enter value for string1:"); string1 = Console.ReadLine(); //output Console.WriteLine("Hello " + string1); } } }online code sample at JDoodle
REFERENCE:https://www.programiz.com/csharp-programming/variables-primitive-data-types
LOAN EXERCISE1: https://www.jdoodle.com/ia/oo0
LOAN APPLICATION EXERCISE: https://docs.google.com/document/d/1ddMMtgv-x9WqyYSG9YncDDeTaT7J97VF0JX67GU_0CQ/
Online coding: https://www.jdoodle.com/compile-c-sharp-online/
Sequential
using System; namespace HelloSequential { public class MySequential { public static void Main(string[] args) { //declare int inputNumber=0; int squaredNumber=0; //input Console.WriteLine("Enter an integer number:"); inputNumber = Convert.ToInt32( Console.ReadLine() ); //process squaredNumber = inputNumber * inputNumber; //output Console.WriteLine("A squared number of " + inputNumber + " is " + squaredNumber); }//main }//class }//namespacerun code at JDoodle
Selection
using System; namespace HelloSelection { public class MySelection { public static void Main(string[] args) { //declare int systemPin=123456; int inputPin=0; //input Console.WriteLine("Enter your pin number:"); inputPin = Convert.ToInt32( Console.ReadLine() ); //process if (inputPin==systemPin){ //output Console.WriteLine("login successful."); }else{ //output Console.WriteLine("login notsuccessful."); } }//main }//class }//namespacerun code at JDoodle
Loop
using System; namespace HelloLoop { public class MyLoop { public static void Main(string[] args) { //declare int systemPin=123456; int inputPin=0; char status='n'; do{ //input Console.WriteLine("Enter your pin number:"); inputPin = Convert.ToInt32( Console.ReadLine() ); //process if (inputPin==systemPin){ //output Console.WriteLine("login successful.\n"); status='y'; }else{ //output Console.WriteLine("login not successful.\n"); } }while(status=='n'); }//main }//class }//namespacerun code at JDoodle
DOC: https://docs.google.com/document/d/11ygg6omVZHlx0fzqG8iIWEq9QbDz5v3Ax3Lq91MAAgM/
Online coding: https://www.jdoodle.com/compile-c-sharp-online/
Switch
using System; namespace HelloPostcode { public class MyPostcode { public static void Main(string[] args) { //declare int inputPostcode=0; string outputCity=""; //input Console.WriteLine("Masukkan poskod:"); inputPostcode=Convert.ToInt32( Console.ReadLine() ); //process switch(inputPostcode){ case 30000: outputCity="ipoh"; break; case 40000: outputCity="selangor"; break; case 50000: outputCity="kuala lumpur"; break; case 80000: outputCity="johor bahru"; break; } //output if (String.IsNullOrEmpty(outputCity)){ Console.WriteLine("Maaf. Bandar tidak dijumpai."); }else{ Console.WriteLine("Bandar yang dicari adalah " + outputCity); } } } }
For Loop
using System; namespace HelloCountry { public class MyCountry { public static void Main(string[] args) { //declare string country="MALAYSIA"; //output for (int i=0; i<country.Length;i++){ Console.WriteLine(country[i]); } } } }
using System; class Program { static void Main() { //declare int[] jadualSifir={ 0,7,14,21,35,42,49,54,63,70,77,84 }; //output //menaik for (int i=0;i<jadualSifir.Length;i++){ Console.Write("7 x "+i+"="+jadualSifir[i]); for (int p=0;p<jadualSifir[i];p++){Console.Write("x");} Console.Write("\n"); } //menurun for (int i=(jadualSifir.Length-1);i>-1;i--){ Console.Write("7 x "+i+"="+jadualSifir[i]); for (int q=0;q<jadualSifir[i];q++){Console.Write("x");} Console.Write("\n"); } } }
DOC: https://docs.google.com/document/d/1GVCv7sr22HFSVvrrEIbVNMDAHyss4gxGdQ7iDWE3yAU/edit?usp=sharing
0 Comments