C# (1-9)

Ace your homework & exams now with Quizwiz!

The basic rules for naming classes in C# are as follows:

- A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or underscore. The first character in an identifier cannot be a digit. - It must not contain any embedded space or symbol such as? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \. However, an underscore ( _ ) can be used. - It should not be a C# keyword.

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

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.

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

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 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.

A C# program consists of the following parts:

- Namespace declaration - A class - Class methods - Class attributes - A Main method - Statements and Expressions - Comments

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.

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.

The varibles in C#, are categorized into the following types:

- Value types - Reference types - Pointer types

Microsoft provides the following development tools for C# programming:

- Visual Studio 2010 (VS) - Visual C# 2010 Express (VCE) - Visual Web Developer

The .Net framework is a revolutionary platform that helps you to write the following types of applications:

- Windows applications - Web applications - Web services

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 multiline comments in C# programs start with /* and terminates with the characters */ as shown below:

/* This program demonstrates The basic syntax of C# programming Language */

C# provides the following built-in type conversion methods:

1) ToBoolean 2) ToByte 3) ToChar 4) ToDateTime 5) ToDecimal 6) ToDouble 7) ToInt16 8) ToInt32 9) ToInt64 10) ToSbyte 11) ToSingle 12) ToString 13) ToType 14) ToUInt16 15) ToUInt32 16) ToUInt64

When the above code is compiled and executed, it produces the following result:

5673

When the above code is compiled and executed, it produces the following result:

75 53.005 2345.7652 True

However, if you want to use these keywords as identifiers, you may prefix the keyword with the _______.

@ character

A @quoted string literal looks as follows:

@"Tutorials Point";

The next line has the namespace declaration.

A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.

Our sample class Rectangle contains three member functions: ____, ______, and _____.

AcceptDetails, GetArea and Display

Pointers in C# have the same capabilities as the pointers in ___ or ___.

C or C++

What is C#?

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg and approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).

The .NET 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 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.

What is C# 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 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.

ToDecimal

Converts a floating point or integer type to a decimal type.

ToDateTime

Converts a type (integer or string type) to date-time structures.

ToInt16

Converts a type to a 16-bit integer.

ToInt32

Converts a type to a 32-bit integer.

ToInt64

Converts a type to a 64-bit integer.

ToByte

Converts a type to a byte

ToDouble

Converts a type to a double type.

ToSbyte

Converts a type to a signed byte type.

ToChar

Converts a type to a single Unicode character, where possible.

ToSingle

Converts a type to a small floating point number.

ToType

Converts a type to a specified type.

ToString

Converts a type to a string.

ToUInt64

Converts a type to an unsigned big integer.

ToUInt16

Converts a type to an unsigned int type.

ToUInt32

Converts a type to an unsigned long type.

When this code is compiled and executed, it produces the following result:

Hello World

For example, let us consider a Rectangle object.

It has attributes such as length and width. Depending upon the design, it may need ways for accepting the values of these attributes, calculating the area, and displaying details.

When the above code is compiled and executed, it produces the following result:

Length: 4.5 Width: 3.5 Area: 15.75

In the preceding program, the class ExecuteRectangle contains the Main() method and instantiates the ______.

Rectangle class

When the above code is compiled and executed, it produces the following result:

Size of int: 4

String Type Example:

String str = "Tutorials Point";

Object is an alias for ________.

System.Object class

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 first statement in any C# program is: using System;

The using keyword is used for including the namespaces in the program. A program can include multiple using statements.

Value type variables can be assigned a value directly.

They are derived from the class System.ValueType.

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.

Type conversion is converting one type of data to another type. It is also known as ________.

Type Casting

An identifier is _______.

a name used to identify a class, variable, function, or any other user-defined item

The .Net framework consists of...

an enormous library of codes used by the client languages such as C#.

The next line /*...*/ is ignored by the compiler

and it is put to add comments in the program.

The String Type allows you to ______.

assign any string values to a variable

Variables are ____ or _____ of a class, used for storing data.

attributes or data members

When a value type is converted to object type, it is called _____ and on the other hand, when an object type is converted to a value type, it is called _____.

boxing; unboxing object obj; obj = 100; // this is boxing

If the data in the memory location is changed by one of the variables, the other variable automatically reflects this _____.

change in value

Pointer Type Example:

char* cptr; int* iptr;

The user-defined reference types are:

class, interface, or delegate.

Dynamic types are similar to object types except that type checking for object type variables takes place at _____, whereas that for the dynamic type variables takes place at _____.

compile time; run time

In C#, some identifiers have special meaning in context of code, such as get and set are called _______.

contextual keywords

ToBoolean

converts a type to a Boolean value, where possible.

The value types directly contain ___.

data

The member functions of a class are ______.

declared within the class

The class keyword is used for _____.

declaring a class

Syntax for declaring a dynamic type is:

dynamic <variable_name> = value; For example: dynamic d = 20;

You can store any type of value in the ______.

dynamic data type variable

Comments are used for _____. Compilers ignore the comment entries.

explaining code

Some examples are ___,____, and ____, which stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

int, char, and float

In the preceding program, the Rectangle class has two member variables named _____ and _____.

length and width

In other words, they refer to a ______. Using multiple variables, the reference types can refer to a memory location.

memory location

The actions that an object may take are called ____.

methods

The .Net framework applications are...

multi-platform applications

A variable is _______.

nothing but a name given to a storage area that our programs can manipulate

The string type is an alias for the System.String class. It is derived from ____.

object type

Example of built-in reference types are:

object, dynamic, and string.

C# is an ______ programming language.

object-oriented

The value for a string type can be assigned using string literals in two forms: _____ and _____ .

quoted and @quoted

The reference types do not contain the actual data stored in a variable, but they contain a ________.

reference to the variables

Keywords are ______.

reserved words predefined to the C# compiler. These keywords cannot be used as identifiers

Type checking for these types of variables takes place at ____.

run-time

Functions are ________.

set of statements that perform a specific task

To get the exact size of a type or a variable on a particular platform, you can use the _____.

sizeof method

The expression _____ yields the storage size of the object or type in bytes.

sizeof(type)

Pointer type variables store _______.

the memory address of another type

Objects of the same kind are said to have_____.

the same type or, are said to be in the same class

The Object Type is ___________.

the ultimate base class for all data types in C# Common Type System (CTS)

The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs _____.

type conversion

Syntax for declaring a pointer type is:

type* identifier;

Following is an example to get the size of int type on any machine:

using System; namespace DataTypeApplication { class Program { static void Main(string[] args) { Console.WriteLine("Size of int: {0}", sizeof(int)); Console.ReadLine(); } } }

Let us look at a simple code that prints the words "Hello World":

using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine("Hello World"); Console.ReadKey(); } } }

Let us look at implementation of a Rectangle class and discuss C# basic syntax:

using System; namespace RectangleApplication { class Rectangle { // member variables double length; double width; public void Acceptdetails() { length = 4.5; width = 3.5; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }

The following example shows an explicit type conversion:

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(); } } }

The following example converts various value types to string type:

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(); } } }

In Object-Oriented Programming methodology, a program consists of _____.

various objects that interact with each other by means of actions.

Single-line comments are indicated by the '//' symbol. For example:

}//end class Rectangle


Related study sets

5. Equal Rights: Struggling Toward Fairness (summer 2020)

View Set

Chapter 65 Oncologic / Degenerative Neurologic Prep U

View Set

Internet & World Wide Web How to Program: Chapter 1

View Set

Chapter 17 - Plate Tectonics Review

View Set

TB - Chapter 24: Assessment of the Skin, Hair, and Nails Chapter 25: Care of Patients with Skin Problems

View Set

The French Revolution and Napoleon Bonaparte

View Set

Organic Chem Chapter 3 Online HW

View Set