C# – Methods

C# – Methods ”; Previous Next A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main. To use a method, you need to − Define the method Call the method Defining Methods in C# When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows − <Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body } Following are the various elements of a method − Access Specifier − This determines the visibility of a variable or a method from another class. Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void. Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class. Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters. Method body − This contains the set of instructions needed to complete the required activity. Example Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. It has public access specifier, so it can be accessed from outside the class using an instance of the class. class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } … } Calling Methods in C# You can call a method using the name of the method. The following example illustrates this − Live Demo using System; namespace CalculatorApplication { class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } static void Main(string[] args) { /* local variable definition */ int a = 100; int b = 200; int ret; NumberManipulator n = new NumberManipulator(); //calling the FindMax method ret = n.FindMax(a, b); Console.WriteLine(“Max value is : {0}”, ret ); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result − Max value is : 200 You can also call public method from other classes by using the instance of the class. For example, the method FindMax belongs to the NumberManipulator class, you can call it from another class Test. Live Demo using System; namespace CalculatorApplication { class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if(num1 > num2) result = num1; else result = num2; return result; } } class Test { static void Main(string[] args) { /* local variable definition */ int a = 100; int b = 200; int ret; NumberManipulator n = new NumberManipulator(); //calling the FindMax method ret = n.FindMax(a, b); Console.WriteLine(“Max value is : {0}”, ret ); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result − Max value is : 200 Recursive Method Call A method can call itself. This is known as recursion. Following is an example that calculates factorial for a given number using a recursive function − Live Demo using System; namespace CalculatorApplication { class NumberManipulator { public int factorial(int num) { /* local variable declaration */ int result; if (num == 1) { return 1; } else { result = factorial(num – 1) * num; return result; } } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); //calling the factorial method {0}”, n.factorial(6)); Console.WriteLine(“Factorial of 7 is : {0}”, n.factorial(7)); Console.WriteLine(“Factorial of 8 is : {0}”, n.factorial(8)); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result − Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320 Passing Parameters to a Method When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method − Sr.No. Mechanism & Description 1 Value parameters This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. 2 Reference parameters This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. 3 Output parameters This method helps in returning more than one value. Print Page Previous Next Advertisements ”;

C# – Strings

C# – Strings ”; Previous Next In C#, you can use strings as array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class. Creating a String Object You can create string object using one of the following methods − By assigning a string literal to a String variable By using a String class constructor By using the string concatenation operator (+) By retrieving a property or calling a method that returns a string By calling a formatting method to convert a value or an object to its string representation The following example demonstrates this − using System; namespace StringApplication { class Program { static void Main(string[] args) { //from string literal and string concatenation string fname, lname; fname = “Rowan”; lname = “Atkinson”; char []letters= { ”H”, ”e”, ”l”, ”l”,”o” }; string [] sarray={ “Hello”, “From”, “Tutorials”, “Point” }; string fullname = fname + lname; Console.WriteLine(“Full Name: {0}”, fullname); //by using string constructor { ”H”, ”e”, ”l”, ”l”,”o” }; string greetings = new string(letters); Console.WriteLine(“Greetings: {0}”, greetings); //methods returning string { “Hello”, “From”, “Tutorials”, “Point” }; string message = String.Join(” “, sarray); Console.WriteLine(“Message: {0}”, message); //formatting method to convert a value DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format(“Message sent at {0:t} on {0:D}”, waiting); Console.WriteLine(“Message: {0}”, chat); } } } When the above code is compiled and executed, it produces the following result − Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, October 10, 2012 Properties of the String Class The String class has the following two properties − Sr.No. Property & Description 1 Chars Gets the Char object at a specified position in the current String object. 2 Length Gets the number of characters in the current String object. Methods of the String Class The String class has numerous methods that help you in working with the string objects. The following table provides some of the most commonly used methods − Given below is the list of methods of the String class. Sr.No. Methods & Description 1 public static int Compare(string strA, string strB) Compares two specified string objects and returns an integer that indicates their relative position in the sort order. 2 public static int Compare(string strA, string strB, bool ignoreCase ) Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true. 3 public static string Concat(string str0, string str1) Concatenates two string objects. 4 public static string Concat(string str0, string str1, string str2) Concatenates three string objects. 5 public static string Concat(string str0, string str1, string str2, string str3) Concatenates four string objects. 6 public bool Contains(string value) Returns a value indicating whether the specified String object occurs within this string. 7 public static string Copy(string str) Creates a new String object with the same value as the specified string. 8 public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) Copies a specified number of characters from a specified position of the String object to a specified position in an array of Unicode characters. 9 public bool EndsWith(string value) Determines whether the end of the string object matches the specified string. 10 public bool Equals(string value) Determines whether the current String object and the specified String object have the same value. 11 public static bool Equals(string a, string b) Determines whether two specified String objects have the same value. 12 public static string Format(string format, Object arg0) Replaces one or more format items in a specified string with the string representation of a specified object. 13 public int IndexOf(char value) Returns the zero-based index of the first occurrence of the specified Unicode character in the current string. 14 public int IndexOf(string value) Returns the zero-based index of the first occurrence of the specified string in this instance. 15 public int IndexOf(char value, int startIndex) Returns the zero-based index of the first occurrence of the specified Unicode character in this string, starting search at the specified character position. 16 public int IndexOf(string value, int startIndex) Returns the zero-based index of the first occurrence of the specified string in this instance, starting search at the specified character position. 17 public int IndexOfAny(char[] anyOf) Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. 18 public int IndexOfAny(char[] anyOf, int startIndex) Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters, starting search at the specified character position. 19 public string Insert(int startIndex, string value) Returns a new string in which a specified string is inserted at a specified index position in the current string object. 20 public static bool IsNullOrEmpty(string value) Indicates whether the specified string is null or an Empty string. 21 public static string Join(string separator, params string[] value) Concatenates all the elements of a string array, using the specified separator between each element. 22 public static string Join(string separator, string[] value, int startIndex, int count) Concatenates the specified elements of a string array, using the specified separator between each element. 23 public int LastIndexOf(char value) Returns the zero-based index position of the last occurrence of the specified Unicode character within the current string object. 24 public int LastIndexOf(string value) Returns the zero-based index position of the last occurrence of a specified string within the current string object. 25 public string Remove(int startIndex) Removes all the characters in the current instance, beginning at a specified position and continuing through the last position, and returns the string. 26 public string Remove(int startIndex, int count) Removes the specified number of characters in the current string beginning at a specified position and returns the string. 27 public string Replace(char oldChar, char newChar) Replaces all occurrences of a specified Unicode character

C# – Decision Making

C# – Decision Making ”; Previous Next Decision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages − C# provides following types of decision making statements. Click the following links to check their detail. Sr.No. Statement & Description 1 if statement An if statement consists of a boolean expression followed by one or more statements. 2 if…else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. 3 nested if statements You can use one if or else if statement inside another if or else if statement(s). 4 switch statement A switch statement allows a variable to be tested for equality against a list of values. 5 nested switch statements You can use one switch statement inside another switch statement(s). The ? : Operator We have covered conditional operator ? : in previous chapter which can be used to replace if…else statements. It has the following general form − Exp1 ? Exp2 : Exp3; Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined as follows: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Print Page Previous Next Advertisements ”;

C# – Variables

C# – Variables ”; Previous Next A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable”s memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable. The basic value types provided in C# can be categorized as − Type Example Integral types sbyte, byte, short, ushort, int, uint, long, ulong, and char Floating point types float and double Decimal types decimal Boolean types true or false values, as assigned Nullable types Nullable data types C# also allows defining other value types of variable such as enum and reference types of variables such as class, which we will cover in subsequent chapters. Defining Variables Syntax for variable definition in C# is − <data_type> <variable_list>; Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas. Some valid variable definitions are shown here − int i, j, k; char c, ch; float f, salary; double d; You can initialize a variable at the time of definition as − int i = 100; Initializing Variables Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is − variable_name = value; Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as − <data_type> <variable_name> = value; Some examples are − int d = 3, f = 5; /* initializing d and f. */ byte z = 22; /* initializes z. */ double pi = 3.14159; /* declares an approximation of pi. */ char x = ”x”; /* the variable x has the value ”x”. */ It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result. The following example uses various types of variables − Live Demo using System; namespace VariableDefinition { class Program { static void Main(string[] args) { short a; int b ; double c; /* actual initialization */ a = 10; b = 20; c = a + b; Console.WriteLine(“a = {0}, b = {1}, c = {2}”, a, b, c); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result − a = 10, b = 20, c = 30 Accepting Values from User The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a variable. For example, int num; num = Convert.ToInt32(Console.ReadLine()); The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format. Lvalue and Rvalue Expressions in C# There are two kinds of expressions in C# − lvalue − An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. rvalue − An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and hence they may appear on the left-hand side of an assignment. Numeric literals are rvalues and hence they may not be assigned and can not appear on the left-hand side. Following is a valid C# statement − int g = 20; But following is not a valid statement and would generate compile-time error − 10 = 20; Print Page Previous Next Advertisements ”;

C# – Home

C# Tutorial Job Search PDF Version Quick Guide Resources Discussion C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. This C# tutorial will teach you basic C# programming and will also take you through various advanced concepts related to C# programming language. C# (pronounced “C sharp“) is widely used for developing different applications, including desktop, web, mobile, cloud, and gaming applications. C# enables developers to build secure and robust.NET applications. Programmers who are familiar with C, C++, Java, and JavaScript can easily understand and work with C# because it adopts basics of C and object oriented programming languages. This tutorial explains the basics of C# programming and then extends to learn its advance concepts also. Why C# – Need of C# C# is a programming language that is both object-oriented and component oriented. C# has built-in language features that directly support to make it robust. This makes it an easy language to use software components. C# has grown over the years by adding new features to support new tasks and new ways of designing software. Several features of C# make applications robust and durable. Followings are some key features that makes C# popular and most in use: Syntax: Because of its syntax”s resemblance to other C-style languages, C# is simple to learn for developers who are already proficient in languages like C, C++, and Java. Object-Oriented: C# supports object-oriented programming paradigms, including encapsulation, inheritance, and polymorphism. Compiled into an intermediate language: Code written in C# is compiled into an intermediate language (IL) that executes on the Common Language Runtime (CLR), an environment that ensures type safety, automated memory management (garbage collection), and exception handling. Platform Independence: The development and execution of C# programs can be facilitated across several platforms, such as Windows, Linux, and macOS using .NET Core or Mono. Language Integrated Query (LINQ): C# incorporates language functionalities that enable type-safe data retrieval from different sources, including databases, XML, and collections. Asynchronous Programming: C# supports asynchronous programming through the async and await keywords, allowing developers to write non-blocking code easily. Rich Standard Library: A powerful standard library (.NET Framework Class Library or .NET Core) in C# provides APIs for file I/O, networking, cryptography, and more. Overall, C# is a flexible programming language that finds extensive usage in enterprise software development, game development (utilizing platforms such as Unity), web development (utilizing ASP.NET), and many other areas. C# Applications – Uses of C# C# is a versatile programming language primarily used for developing software on the Microsoft platform. Some of the common applications of C# are as follows: Desktop Applications: Frameworks like Windows Presentation Foundation (WPF) or Windows Forms are often used with C# to make Desktop applications. These programs can be simple utilities to complex enterprise software. Web Applications: The C# programming language is used to develop web applications using ASP.NET and ASP.NET Core frameworks. Developers can use these frameworks to develop web applications, encompassing e-commerce websites, content management systems, and web APIs. Mobile Applications: Xamarin is a framework that may be used to create mobile applications for iOS, Android, and Windows Phone using C#. Xamarin facilitates the development of cross-platform mobile applications using C#, it also enables developers to share code across different platforms. Game Development: C# is used a lot in the game creation industry, particularly with the Unity game engine. Unity enables developers to build 2D and 3D games that can be played on desktop computers, mobile devices, and game consoles. Enterprise Software: It”s easy to use C# to make business software like customer relationship management (CRM) systems, enterprise resource planning (ERP) software, and business data apps. Cloud Services: With the development of cloud computing, C# is currently being used to create cloud-based apps and services. Microsoft Azure provides a broad range of services and tools for building and deploying C# apps on the cloud. Internet of Things (IoT): C# can be used to create software for IoT devices using platforms such as Windows IoT Core or building applications that operate on different IoT devices using .NET Core frameworks. Machine Learning and Data Analysis: Machine learning and data analysis tasks can be effectively performed using C#, which makes use of libraries such as ML.NET and frameworks like Microsoft Azure Machine Learning. Tools and Utilities: C# is frequently employed in the development of tools and utilities pertaining to the needs of developers, system administrators, and other professionals in technical fields. The aforementioned tools encompass code editors, debuggers, performance monitoring tools etc. Financial Applications: The C# programming language is commonly used in financial sector for the development of trading platforms, risk management systems, algorithmic trading tools, and various other financial applications. Because of its adaptability, robust community support, and interaction with the Microsoft environment, C# is a preferred language for different software development activities. Audience This C# tutorial has been prepared for those who want to learn about the basics and advance functions of C# programming language. It is specifically useful in Desktop Applications, Web Applications, Mobile Applications, Game Development, Cloud Services, Internet of Things (IoT), Machine Learning, and Data Analysis, Tools and Utilities and other related domains where application development requires. After completing this tutorial, you will find yourself at a moderate level of expertise from where you can take yourself to higher levels of expertise in C# programming. Prerequisites Before learning this C# tutorial, you should have a basic understanding of computer programming terminologies. A basic understanding of C, C++ and object oriented programming and leaning of any of the object oriented programming languages like Java is a plus. Print Page Previous Next Advertisements ”;

C# – Structure

C# – Structures ”; Previous Next In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure. Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title Author Subject Book ID Defining a Structure To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. For example, here is the way you can declare the Book structure − struct Books { public string title; public string author; public string subject; public int book_id; }; The following program shows the use of the structure − Live Demo using System; struct Books { public string title; public string author; public string subject; public int book_id; }; public class testStructure { public static void Main(string[] args) { Books Book1; /* Declare Book1 of type Book */ Books Book2; /* Declare Book2 of type Book */ /* book 1 specification */ Book1.title = “C Programming”; Book1.author = “Nuha Ali”; Book1.subject = “C Programming Tutorial”; Book1.book_id = 6495407; /* book 2 specification */ Book2.title = “Telecom Billing”; Book2.author = “Zara Ali”; Book2.subject = “Telecom Billing Tutorial”; Book2.book_id = 6495700; /* print Book1 info */ Console.WriteLine( “Book 1 title : {0}”, Book1.title); Console.WriteLine(“Book 1 author : {0}”, Book1.author); Console.WriteLine(“Book 1 subject : {0}”, Book1.subject); Console.WriteLine(“Book 1 book_id :{0}”, Book1.book_id); /* print Book2 info */ Console.WriteLine(“Book 2 title : {0}”, Book2.title); Console.WriteLine(“Book 2 author : {0}”, Book2.author); Console.WriteLine(“Book 2 subject : {0}”, Book2.subject); Console.WriteLine(“Book 2 book_id : {0}”, Book2.book_id); Console.ReadKey(); } } When the above code is compiled and executed, it produces the following result − Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700 Features of C# Structures You have already used a simple structure named Books. Structures in C# are quite different from that in traditional C or C++. The C# structures have the following features − Structures can have methods, fields, indexers, properties, operator methods, and events. Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed. Unlike classes, structures cannot inherit other structures or classes. Structures cannot be used as a base for other structures or classes. A structure can implement one or more interfaces. Structure members cannot be specified as abstract, virtual, or protected. When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized. Class versus Structure Classes and Structures have the following basic differences − classes are reference types and structs are value types structures do not support inheritance structures cannot have default constructor In the light of the above discussions, let us rewrite the previous example − Live Demo using System; struct Books { private string title; private string author; private string subject; private int book_id; public void getValues(string t, string a, string s, int id) { title = t; author = a; subject = s; book_id = id; } public void display() { Console.WriteLine(“Title : {0}”, title); Console.WriteLine(“Author : {0}”, author); Console.WriteLine(“Subject : {0}”, subject); Console.WriteLine(“Book_id :{0}”, book_id); } }; public class testStructure { public static void Main(string[] args) { Books Book1 = new Books(); /* Declare Book1 of type Book */ Books Book2 = new Books(); /* Declare Book2 of type Book */ /* book 1 specification */ Book1.getValues(“C Programming”, “Nuha Ali”, “C Programming Tutorial”,6495407); /* book 2 specification */ Book2.getValues(“Telecom Billing”, “Zara Ali”, “Telecom Billing Tutorial”, 6495700); /* print Book1 info */ Book1.display(); /* print Book2 info */ Book2.display(); Console.ReadKey(); } } When the above code is compiled and executed, it produces the following result − Title : C Programming Author : Nuha Ali Subject : C Programming Tutorial Book_id : 6495407 Title : Telecom Billing Author : Zara Ali Subject : Telecom Billing Tutorial Book_id : 6495700 Print Page Previous Next Advertisements ”;

C# – Type Conversion

C# – Type Conversion ”; Previous Next Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms − Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes. Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator. The following example shows an explicit type conversion − Live Demo using System; namespace TypeConversionApplication { class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // cast double to int. i = (int)d; Console.WriteLine(i); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result − 5673 C# Type Conversion Methods C# provides the following built-in type conversion methods − Sr.No. Methods & Description 1 ToBoolean Converts a type to a Boolean value, where possible. 2 ToByte Converts a type to a byte. 3 ToChar Converts a type to a single Unicode character, where possible. 4 ToDateTime Converts a type (integer or string type) to date-time structures. 5 ToDecimal Converts a floating point or integer type to a decimal type. 6 ToDouble Converts a type to a double type. 7 ToInt16 Converts a type to a 16-bit integer. 8 ToInt32 Converts a type to a 32-bit integer. 9 ToInt64 Converts a type to a 64-bit integer. 10 ToSbyte Converts a type to a signed byte type. 11 ToSingle Converts a type to a small floating point number. 12 ToString Converts a type to a string. 13 ToType Converts a type to a specified type. 14 ToUInt16 Converts a type to an unsigned int type. 15 ToUInt32 Converts a type to an unsigned long type. 16 ToUInt64 Converts a type to an unsigned big integer. The following example converts various value types to string type − Live Demo using System; namespace TypeConversionApplication { class StringConversion { static void Main(string[] args) { int i = 75; float f = 53.005f; double d = 2345.7652; bool b = true; Console.WriteLine(i.ToString()); Console.WriteLine(f.ToString()); Console.WriteLine(d.ToString()); Console.WriteLine(b.ToString()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result − 75 53.005 2345.7652 True Print Page Previous Next Advertisements ”;

C# – Program Structure

C# – Program Structure ”; Previous Next Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters. Creating Hello World Program A C# program consists of the following parts − Namespace declaration A class Class methods Class attributes A Main method Statements and Expressions Comments Let us look at a simple code that prints the words “Hello World” − Live Demo using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine(“Hello World”); Console.ReadKey(); } } } When this code is compiled and executed, it produces the following result − Hello World Let us look at the various parts of the given program − The first line of the program using System; – the using keyword is used to include the System namespace in the program. A program generally has multiple using statements. The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld. The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main. The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed. The next line /*…*/ is ignored by the compiler and it is put to add comments in the program. The Main method specifies its behavior with the statement Console.WriteLine(“Hello World”); WriteLine is a method of the Console class defined in the System namespace. This statement causes the message “Hello, World!” to be displayed on the screen. The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET. It is worth to note the following points − C# is case sensitive. All statements and expression must end with a semicolon (;). The program execution starts at the Main method. Unlike Java, program file name could be different from the class name. Compiling and Executing the Program If you are using Visual Studio.Net for compiling and executing C# programs, take the following steps − Start Visual Studio. On the menu bar, choose File -> New -> Project. Choose Visual C# from templates, and then choose Windows. Choose Console Application. Specify a name for your project and click OK button. This creates a new project in Solution Explorer. Write code in the Code Editor. Click the Run button or press F5 key to execute the project. A Command Prompt window appears that contains the line Hello World. You can compile a C# program by using the command-line instead of the Visual Studio IDE − Open a text editor and add the above-mentioned code. Save the file as helloworld.cs Open the command prompt tool and go to the directory where you saved the file. Type csc helloworld.cs and press enter to compile your code. If there are no errors in your code, the command prompt takes you to the next line and generates helloworld.exe executable file. Type helloworld to execute your program. You can see the output Hello World printed on the screen. Print Page Previous Next Advertisements ”;

C# – Overview

C# – Overview ”; Previous Next C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). C# was developed by Anders Hejlsberg and his team during the development of .Net Framework. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures. The following reasons make C# a widely used professional language − It is a modern, general-purpose programming language It is object oriented. It is component oriented. It is easy to learn. It is a structured language. It produces efficient programs. It can be compiled on a variety of computer platforms. It is a part of .Net Framework. Strong Programming Features of C# Although C# constructs closely follow traditional high-level languages, C and C++ and being an object-oriented programming language. It has strong resemblance with Java, it has numerous strong programming features that make it endearing to a number of programmers worldwide. Following is the list of few important features of C# − Boolean Conditions Automatic Garbage Collection Standard Library Assembly Versioning Properties and Events Delegates and Events Management Easy-to-use Generics Indexers Conditional Compilation Simple Multithreading LINQ and Lambda Expressions Integration with Windows Print Page Previous Next Advertisements ”;

C# – Environment

C# – Environment ”; Previous Next In this chapter, we will discuss the tools required for creating C# programming. We have already mentioned that C# is part of .Net framework and is used for writing .Net applications. Therefore, before discussing the available tools for running a C# program, let us understand how C# relates to the .Net framework. The .Net Framework The .Net framework is a revolutionary platform that helps you to write the following types of applications − Windows applications Web applications Web services The .Net framework applications are multi-platform applications. The framework has been designed in such a way that it can be used from any of the following languages: C#, C++, Visual Basic, Jscript, COBOL, etc. All these languages can access the framework as well as communicate with each other. The .Net framework consists of an enormous library of codes used by the client languages such as C#. Following are some of the components of the .Net framework − Common Language Runtime (CLR) The .Net Framework Class Library Common Language Specification Common Type System Metadata and Assemblies Windows Forms ASP.Net and ASP.Net AJAX ADO.Net Windows Workflow Foundation (WF) Windows Presentation Foundation Windows Communication Foundation (WCF) LINQ For the jobs each of these components perform, please see ASP.Net – Introduction, and for details of each component, please consult Microsoft”s documentation. Integrated Development Environment (IDE) for C# Microsoft provides the following development tools for C# programming − Visual Studio 2010 (VS) Visual C# 2010 Express (VCE) Visual Web Developer The last two are freely available from Microsoft official website. Using these tools, you can write all kinds of C# programs from simple command-line applications to more complex applications. You can also write C# source code files using a basic text editor, like Notepad, and compile the code into assemblies using the command-line compiler, which is again a part of the .NET Framework. Visual C# Express and Visual Web Developer Express edition are trimmed down versions of Visual Studio and has the same appearance. They retain most features of Visual Studio. In this tutorial, we have used Visual C# 2010 Express. You can download it from Microsoft Visual Studio. It gets installed automatically on your machine. Note: You need an active internet connection for installing the express edition. Writing C# Programs on Linux or Mac OS Although the.NET Framework runs on the Windows operating system, there are some alternative versions that work on other operating systems. Mono is an open-source version of the .NET Framework which includes a C# compiler and runs on several operating systems, including various flavors of Linux and Mac OS. Kindly check Go Mono. The stated purpose of Mono is not only to be able to run Microsoft .NET applications cross-platform, but also to bring better development tools for Linux developers. Mono can be run on many operating systems including Android, BSD, iOS, Linux, OS X, Windows, Solaris, and UNIX. Print Page Previous Next Advertisements ”;