C#------Intro
With introduction to .net framework Microsoft introduced a new language c#(pronounced c sharp).C# is designed to be a simple,modern,general purpose,object oriented programming language. Application written in c# requires the .net framework to be installed on the computer running the application.Sometimes it is referred to as .Net language perhaps it was designed together with the .Net framework.C# is pure object oriented programming language and doesn't offer global variables or functions.Everything is wrapped in classes,even simple types like int and string, which inherits from the System.object.class.
A simple program:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } }when we open a console application that looks like above now we write our first program.
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello, world!"); Console.ReadLine(); } } }Now, hit f5 to run this program and see the output.
Let's start with the shortest and most common characters in our code: The { and }. They are often referred to as curly braces, and in C#, they mark the beginning and end of a logical block of code. The curly braces are used in lots of other languages, including C++, Java, JavaScript and many others. As you can see in the code, they are used to wrap several lines of code which belongs together.
static void Main(string[] args)Above line is probably the most complicated one in this example, so let's split it up a bit. The first word is static. The static keyword tells us that this method should be accesible without instantiating the class, but more about this in our chapter about classes. The next keyword is void, and tells us what this method should return. For instance, int could be an integer or a string of text, but in this case, we don't want our method to return anything, or void, which is the same as no type. The next word is Main, which is simply the name of our method. This method is the so-called entry-point of our application, that is, the first piece of code to be executed, and in our example, the only piece to be executed. Now, after the name of a method, a set of arguments can be specified within a set of parentheses. In our example, our method takes only one argument, called args. The type of the argument is a string, or to be more precise, an array of strings, but more on that later. If you think about it, this makes perfect sense, since Windows applications can always be called with an optinal set of arguments. These arguments will be passed as text strings to our main method.
C# is a strongly typed language.
Data Type in C#:------
bool ----is one of the simplest data types. It can contain only 2 values - false or true. The bool type is important to understand when using logical operators like the if statement.
int ----is short for integer, a data type for storing numbers without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C#, depending on the size of the number they are supposed to store.
string ----is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings are never changed after they have been created. When using methods which changes a string, the actual string is not changed - a new string is returned instead.
char ----is used for storing a single character.
float---- is one of the data types used to store numbers which may or may not contain decimals.
int ----is short for integer, a data type for storing numbers without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C#, depending on the size of the number they are supposed to store.
string ----is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings are never changed after they have been created. When using methods which changes a string, the actual string is not changed - a new string is returned instead.
char ----is used for storing a single character.
float---- is one of the data types used to store numbers which may or may not contain decimals.
Variable in C#
A variable can be compared to a storage room, and is essential for the programmer. In C#, a variable is declared like this:
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string firstName = "John"; string lastName = "Doe"; Console.WriteLine("Name: " + firstName + " " + lastName); Console.WriteLine("Please enter a new first name:"); firstName = Console.ReadLine(); Console.WriteLine("New name: " + firstName + " " + lastName); Console.ReadLine(); } } }Another interesting example is doing math. Here is one, based on a lot of the same code we have just used:
int number1, number2; Console.WriteLine("Please enter a number:"); number1 = int.Parse(Console.ReadLine()); Console.WriteLine("Thank you. One more:"); number2 = int.Parse(Console.ReadLine()); Console.WriteLine("Adding the two numbers: " + (number1 + number2)); Console.ReadLine();
No comments:
Post a Comment