Part 1 to 10 Basic in C #,

Source: Internet
Author: User

Part 1 to 10 Basic in C #,

Reference page:

Http://www.yuanjiaocheng.net/CSharp/csharp-partial-class.html

Http://www.yuanjiaocheng.net/CSharp/csharp-Static.html

Http://www.yuanjiaocheng.net/CSharp/csharp-Anonymous-method.html

Http://www.yuanjiaocheng.net/CSharp/csharp-nullable.html

Http://www.yuanjiaocheng.net/CSharp/csharp-var.html

Part 1 Introduction

The struct of C # program:

Namespace, class and Main method

What is namespace?

The namespace declaration, using System, indicates that you are using the System namespace.

A namespace is used to organize your code and is collection of classes, interfaces, structs, enums and delegates.

Main method is the entry point into your application

Part 2 Reading and writing to a console

Reading from the console and Writing to the console

2 ways to write to console

A) Concatenation (+)

B) Place holder syntax-Most Preferred (placeholder, this type is recommended)

C # is case sensitive (case sensitive)

Part 3 Built-in types

Boolean type -- Only true or false

Integral Types -- sbyte, byte, short, ushort, int, uint, long, ulong, char. how to know Integral Types Value? Using MinValue and MaxValue property

Floating Types -- float and double, double type is default type, if you want to declare a float type, you shoshould add F at end, else, it will get an error

Decimal Types

String Type

Part 4 String type in c #

Talk about escape \ n, \ ', \ "and etc. sometimes using @ to not escape (escape)

Part 5 Common Operators in C #

Assignment operator = (Value Assignment operator)

Arithmetic operator +,-, *,/, %

Comparison operator like = ,! =,>, >=, <=

Conditional operator like &, |

Ternary operator? :

Null coalescing operator ??

Part 6 Nullable Types

Types in C #

In C # types are divided into 2 broad categories

Value Types -- int, float, double, structs, enums etc

Reference Types -- Interface, Class, Delegetes, arrays etc

By default value types are non nullable. To make them nullable use?

Int I = 0 (I is non nullable, so I cannot be set to null, I = null will generate compiler error)

Int? J = 0 (j is nullable int, so j = null is legal)

Nullable types bridge the differences between C # types and Database types

About ?? Operator, eg: int? I = 100 or null, int j = I ?? 0. it means if I is null, then j = 0, else, j = I

Part 7 Datatypes conversion

Implicit conversions (Implicit)

Explicit conversions (Explicit)

Difference between Parse () and TryParse ()

Implicit & explicit conversion

Implicit conversion is done by the complier:

1, when there is no loss of information if the conversion is done

2, if there is no possibility of throwing exceptions during the conversion

Converting an int to a float will not loose any data and no exception will be thrown, hence (SO) an implicit conversion can be done.

Implicit Conversion Example:

int a = 10;float b = a;//float is bigger datatype than int, so, no loss of data and exceptions, hence this is implicit conversion.

 

Where as when converting a float to an int, we loose the fractional (number of decimal points) part and also a possibility of overflow exception. hence, in this case an explicit conversion is required. for explicit conversion we can use cast operator or the convert class in C #

Example:

float a =3.14F;int b =(int)a;//use explicit conversion using cast() operatorint b = Convert.ToInt32(a);//use convert class

 

Diffrence between Parse (resolution) and TryParse

If the number is in a string format you have 2 options -- int. Parse () and int. TryParse ()

Parse () method throws an exception if it cannot parse the value, where as TryParse () returns a bool indicating whether it succeeded or failed, so, Use Parse () if you are sure the vlue will be valid, Otherwise, use TryParse ()

Example:

string a ="100";string a1="100e";int result = 0;int b = int.Parse(a);//it will be success.int b1 =int.Parse(a1) //it will throw an exception.bool isConvertSuccessful= int.TryParse(a1,out result);//no matter convert success or fail,it will not throw an exception.if(isConvertSuccessful)    Consolve.Write(result);else    Consolve.Write("convert fail");

 

Part 8 Arrays in C #

An array is a collection of similar data types.

Advantages: Arrays are stronugly typed.

Disadvantages: Arrays cannot grow in size once initialized.

Have to rely on integral indices to store or retrieve items from the array. (You must rely on indexes to store or retrieve items from the array .)

Example for declare an array:

int[] numbers = new int[3];numbers[1]=1;numbers[2]=2;numbers[3]=3;int[] numbers1 = {1,2,3};

 

Part 9 Comments in C #

Single line Comments -//

Multi line Comments -/**/

XML Documentation Comments-// if using this comment, when you mouseover it, it can show comment to you

Comment are used to document what the program does and what specific blocks or lines of code do. C # complier ignores comments

To Comment and Uncomment, there are 2 ways

1, Use the designer

2. Keyboard Shortcut cut: use Ctrl + K or Ctrl + C to comment and use Ctrl + K or Ctrl + U to uncomment

Note: Don't try to comment every line of code. User comments only for blocks or lines of code that are difficult to understand

Part 10 If statement in C #

If statement

If else statement

Difference between & and &

Difference between | and |

Take example:

int number = int.TryParse(Cosole.ReadLine());if(number ==0 && number ==10)  //if number >0,it will not exce condition number ==10if(number ==0 & number ==10)  // though number>0 it will exce condition number==10,so, it is one more step than &&. the meaning about || and | also like that.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.