Вы находитесь на странице: 1из 13

C # (pronounced c-sharp)

The C# programming language is one of those intermediate languages that programmers use to create executable programs. Programming language developed at Microsoft in 2000. It was designed by a team at Microsoft led by Anders Hejlsberg. Its the official language of choice at Microsoft for its development actives, a C# program file carries the extension .cs

Features :
Flexible: C# programs can execute on the current machine, or they can be transmitted over the Web and executed on some distant computer. Powerful: C# has essentially the same command set as C++ but with the rough edges filed smooth. Easier to use: C# error-proofs the commands responsible for most C++ errors, so you spend far less time chasing down those errors. Visually oriented: Readily creates complicated display frames with drop-down lists, tabbed windows, grouped buttons, scroll bars, and background images, to name just a few. Internet-friendly: C# plays a pivotal role in the .NET Framework, Microsofts current approach to programming for Windows, the Internet, and beyond. Secure: Any language intended for use on the Internet must include serious security to protect against hackers.

What is .net .net is a software development platform created by Microsoft .net framework has two parts. FCL- frame class library : collection of ready-made classes that can be used for commonly needed programming tasks. It is very versatile and helps carry out numerous tasks while creating software.

CLR- Common Language Runtime , responsible for execution of c# programs, and handles memory security while executing a c# program. Program written in a language cannot be executed directly and need to be converted machine language instructions. .net performs this in 2 steps . 1st the program is converted into language instructions in MSIL (Microsoft intermediate language). 2nd MSIL is converted in to machine language.

Visual studio:
For building software in any language you need developing tools. Editor( to type a program), Compiler (to compile the program), Debugger ( detect, analyze, and eliminate bugs) Instead of using this program separately, it comes together in an Integrated Development Environment IDE Visual studio is an IDE very popularly used to building programs in c#, VB.net and visual c++.

Data types:
A data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored. Based on where data type can be created in a memory, it is called value type and reference type Predefined is also called as Primitives. A data type specifies 3 things. a) what value can data type take.

b) How many bytes would data type occupy in the memory. c) What operations can be performed on data type.

Constant and variable :


To use the data type in the program , we have to create constants and variables. Constant/literal : a specific value from the range offered by the data type. unsigned values are considered positive. a float constant must be followed by a suffix f. A float constant can be expressed in fractional form (1.234f) or in exponential form (1.234e5). A character constant is a single alphabet, digit, special symbols, enclosed with singe inverter commas, both inverted commas should be pointed to the left. Example : a is valid , but a is invalid.

Variable/identifier: the container which can hold the constant value , container is typically the memory location. Variable name is a combination of 1 to 31 characters. 1st character should start with an alphabet or underscore. Commas or blanks or special symbols (except underscore) are not allowed with in variable name. Variable names are character sensitive.

C# Keywords:
Keywords are the pre-defined words in c# compiler. Keywords cannot be used as variable name , if you are still keen on using the keyword as a name, precede the word with @. Examples, @if @int @while

The first c# program :


using System; namespace siproject { class simpleinterest { static void Main(string[] args) { float p, r, si; int n; p = 23.5f; r = 45.3f; n = 3; si = p * n * r / 100; Console.WriteLine(si); } } } The statement in the program must appear in the same order in which we wish them to be executed. No blank space is allowed between names of variable, constant or keyword. Every c# statement must end with a ; , thus ; acts as a statement terminator. Comments about the program should be enclosed with /*_ _ _ _*/ or starts with // Comments cannot be nested. Example: // comment1 /*comment2*/ Any variable used in the program must be declared before using. There are lot of operators in c# , like +, -, *.. Etc, there are totally 55 operations available. //float values end with f /*float values end with f*/

WritLine():
It is necessary to add using system at the beginning of the program. We can print multiple values using writeline(), example : Console.WriteLine(si=+si); or Console.WriteLine("si="+si+"p="+p+"r="+r); To go to the next line we use \n. Example Console.WriteLine("si="+si+\n+"p="+p+"r="+r); It can also print the values of the valid expressions. Example Console.WriteLine(p=+p+(p+r));

ReadLine():
using System; namespace siproject { class simpleinterest { static void Main(string[] args) { float p, r, si; int n; Console.WriteLine("enter values for p,n and r"); p = Convert.ToSingle(Console.ReadLine()); n = Convert.ToInt32(Console.ReadLine()); r = Convert.ToSingle(Console.ReadLine()); si = p * n * r / 100; Console.WriteLine(si); } } }

The values entered by the keyboard are read by the readline function as strings, i.e. as a collections of characters, rather than as numbers. Therefore a conversion is required. Tosingle(): converts sting to real number. Single precision real number i.e a float Toint32(): converts strings to integer. Int32 is 32-bit integer

Write.Line can also be used for different purpose. Example : int x, y; float a; x = 6; y = 4; a = x + y; Console.WriteLine("x={0} y={1} a={2}",x,y,a);

Types of data :
Integer type : by default number without a decimal point is treated as an int. Real types: Real number can be represented as float, double or decimal.By default a number with the point is taken as double. Double to float - - - suffix f or F Double to decimal- - suffix m or M Integer number to be treated as double then use suffix d or D.

Compared to float and double the decimal type has more precision. Numbers can be represented in exponential form. Float a= 1.23e-5f; double a=3.45e3; Bool data type: Bool data type can take boolean values, true and false. Example: Bool a= false; Console.WriteLine(a=+a); Console.WriteLine(result=+(4>3)); the output would be , a=false; result=true

Unlike languages like c and c++ , in C# true is not 1 and false is not 0.

Type declaration instruction :


This instruction is used to declare the type of variable used, any variable used in the program must be declared before using it in any statement. While declaring the variable we can also initialize it. Int i=10, r=45; The order sometimes matters. float a=1.5, b=a+3; but float b=a+3, a=1.5; int a,b,c,d; a=b=c=d=10; but int a=b=c=d=10; is not valid.

Implicit and Explicit form of operations:


Implicit form, int y; float x,a; x = 6.0f; y = 4; a = x / y; Console.WriteLine("a="+a); Explicit form, int x, y; float a; x = 6; y = 4; a = (float)x / y; Console.WriteLine("a="+a);

Arithmetic instruction :
Arithmetic instructions are the expressions connected by the arithmetic operations like +, -, *, / . The variables and constants are together called operands. Arithmetic statements can be of 3 types. Integer mode statement. Real mode arithmetic statement. Mixed mode arithmetic statement. During arithmetic operation only one variable should be present in the LHS. % is called modular division operator which returns the reminder. % can also be done on floats. If a and b are floats then a%b is computed as follows.. find the largest possible integer less than or equal to a/b , and let it be n. And a%b = (a-n)/b No operator is assumed to be present example: 5a not= 5*a. Exponential operation : double a; a=Math.pow(3.0,2.0); Console.WriteLine(a); Double value should be used with Pow. There are other mathematical functions like , sqrt(),abs(), sin(), cos(), tan(), log(), min(), max(),.. Etc.

Type conversions in arithmetic instructions : Operation between int and int always yields an int results. Operation between real and real always yields a real result. Operation between int and real always yields a real result.

Implicit conversion rules : Operation between any two integers except long and ulong or char results into an int. A char can be implicitly converted to short,ushort, int, uint, long, ulong, float, or decial. But the other way is not possible. There are no implicit conversions between floating point and decimal type.

Explicit conversion : At times we are required to explicitly convert one type into another. Example float a; int x=6,y=4; a=x/y; Console.WriteLine(answer=+a); output would look like : answer=1 Instead convert explicitly as, a=(float)x/y;

Вам также может понравиться