0% found this document useful (0 votes)
17 views3 pages

C# 12.0

The document provides an overview of C# language basics, including namespaces, compilation, literals, operators, comments, and types. It explains the differences between value types and reference types, as well as string manipulation techniques like concatenation and interpolation. Additionally, it covers arrays, their declaration, and the use of indices and ranges introduced in C# 8.

Uploaded by

venkatmalepati9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

C# 12.0

The document provides an overview of C# language basics, including namespaces, compilation, literals, operators, comments, and types. It explains the differences between value types and reference types, as well as string manipulation techniques like concatenation and interpolation. Additionally, it covers arrays, their declaration, and the use of indices and ranges introduced in C# 8.

Uploaded by

venkatmalepati9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

C# Language Basics :
===================================================================================
=
Many commonly used types—including the Console class—reside in the System
namespace.
System.Text namespace contains types for handling text, and System.IO contain types
for input output.

Compilation :

The C# compiler compiles source code (a set of files with the .cs extension) into
an assembly. An assembly is the unit of packaging and deployment in .NET.
An assembly can be either an application or a library.

Literals, Punctuators, and Operators :

Literals are primitive pieces of data lexically embedded into the


program.Theliterals we used in our example program are 12 and 30.

Punctuators help demarcate the structure of the program. An example is the


semicolon,which terminates a statement. Statements can wrap multiple lines:
Console.WriteLine (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10);

An operator transforms and combines expressions. Most operators in C# are denoted


with a symbol, such as the multiplication operator, *.
A period denotes a member of something (or a decimal point with numeric
literals).
Parentheses are used when declaring or calling a method;
empty parentheses are used when the method accepts no arguments.
An equals sign performs assignment.

Comments
C# offers two different styles of source-code documentation: single-line
comments and multiline comments.
A single-line comment begins with a double forward slash and continues until
the end of the line;

Type Basics
A type defines the blueprint for a value.
In this example, we use two literals of type int with values 12 and 30.
We also declare a variable of type int whose name is x: int x = 12 * 30;
Console.WriteLine (x);

A variable denotes a storage location that can contain different values over
time. In contrast, a constant always represents the same value. const int y = 360;

Predefined Type Examples

Predefined types are types that are specially supported by the compiler.
The int type is a predefined type for representing the set of integers that
fit into 32 bits of memory, from −231 to 231 − 1, and is the default type for
numeric literals within this range.
The string type represents a sequence of characters.
The predefined bool type has exactly two possible values: true and false.

Custom Types
Just as we can write our own methods, we can write our own types. In this
next example, we define a custom type named UnitConverter—a class that serves as a
blueprint for unit conversions:
An array (such as string[]) represents a fixed number of elements of a particular
type. Arrays are specified by placing square brackets after the element type.

Types and Conversions


C# can convert between instances of compatible types.
A conversion always creates a new value from an existing one. Conversions can
be either implicit or explicit:implicit conversions happen automatically, and
explicit conversions require a cast.
In the following example, we implicitly convert an int to a long type (which
has twice the bit capacity of an int) and explicitly cast an int to a short type
(which has half the bit capacity of an int):
int x = 12345; // int is a 32-bit integer
long y = x; // Implicit conversion to 64-bit integer
short z = (short)x; // Explicit conversion to 16-bit integer

Value Types Versus Reference Types


All C# types fall into the following categories:
• Value types
• Reference types
• Generic type parameters
• Pointer types

Value-type instances occupy precisely the memory required to store


their fields
Reference types require separate allocations of memory for the
reference and object.
The object consumes as many bytes as its fields, plus additional
administrative overhead. The precise overhead is intrinsically private to the
implementation of the .NET runtime, but at minimum, the overhead is 8 bytes, used
to store a key to the object’s type as well as temporary information such as its
lock state for multithreading and a flag to indicate whether it has been fixed from
movement by the garbage collector. Each reference to an object requires an extra 4
or 8 bytes,depending on whether the .NET runtime is running on a 32- or 64-bit
platform.

String concatenation: The + operator concatenates two strings


String interpolation: A string preceded with the $ character is called an
interpolated string.
Interpolated strings can include expressions enclosed in braces:
int x = 4;
Console.Write ($"A square has {x} sides"); // Prints: A square has 4
sides

Constant interpolated strings (C# 10)


From C# 10, interpolated strings can be constants, as long as the
interpolated values are constants:
const string greeting = "Hello";
const string message = $"{greeting}, world";

Arrays
An array represents a fixed number of variables (called elements) of a
particular type. The elements in an array are always stored in a contiguous block
of memory,providing highly efficient access. An array is denoted with square
brackets after the element type:
char[] vowels = new char[5]; // Declare an array of 5 characters
Square brackets also index the array, accessing a particular element by
position:
vowels[0] = 'a';
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
Console.WriteLine (vowels[1]); // e
An array itself is always a reference type object, regardless of the element
type. For instance,
the following is legal: int[] a = null;

Indices and Ranges


Indices and ranges (introduced in C# 8) simplify working with elements or
portions of an array.
C# implements indices with the help of the Index type, so you can also do the
following:
Index first = 0;
Index last = ^1;
char firstElement = vowels [first]; // 'a'
char lastElement = vowels [last]; // 'u'

You might also like