C# Programming
Nullables-String-enum-
Struct
Nullables
a C# special data types.
you can assign normal range of values as well
as null values.
For example, you can store any value from
-2,147,483,648 to 2,147,483,647 or null in a
Nullable<Int32> variable. Similarly, you can
assign true, false, or null in a Nullable<bool>
variable.
Syntax for declaring a nullable type is as follows
−
< data_type> ? <variable_name> = null;
Example
using System;
class NullablesAtShow {
static void Main(string[] args)
{
int ? num1 = null;
int ? num2 = 45;
double ? num3 = new double?();
double ? num4 = 3.14157;
bool ? boolval = new bool?();
Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", num1, num2, num3, num4);
Console.WriteLine("A Nullable boolean value: {0}", boolval); Console.ReadLine();
} }
String
strings are array of characters.
Use the string keyword to declare a string variable.
The string keyword is an alias for
the System.String class.
char []letters= { 'H', 'e', 'l', 'l','o' };
string fname = "Rowan";
string [] sarray={ "Hello", "From", "Tutorials", "Point" };
Creating string object
By assigning a string literal to a String variable
string fname, lname;
fname = "Rowan"; lname = "Atkinson";
By using the string concatenation operator (+)
string fullname = fname + lname;
By using a String class constructor
char []letters= { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Creating string object (cont.)
By calling a method that returns a string
string [] sarray={ "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
By calling a formatting method to convert a value
or an object to its string representation
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);
Some methods of the String class.
Some methods of the String class.
Some methods of the String class.
Some methods of the String class.
Structures
Structures
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.
Example
Features of C# Structures
Structures can have methods, fields, indexers,
properties, operator methods, and events.
Structures can have defined constructors, but not
destructors.
Unlike classes, structures cannot inherit other structures
or classes.
A structure can implement one or more interfaces.
Structure members cannot be specified as abstract,
virtual, or protected.
Unlike classes ,structs can be instantiated without using
the New operator.
classes are reference types and structs are value types
structures cannot have default constructor.
Enums
Enumeration
An enumeration is a set of named integer constants.
An enumerated type is declared using the enum keyword.
C# enumerations are value data type
cannot inherit or cannot pass inheritance.
Declaring enum Variable
enum <enum_name> { enumeration list };
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
{0, 1, 2 , 3 , 4 , 5 , 6 }
Each of the symbols in the enumeration list stands for an
integer value, By default, the value of the first
enumeration symbol is 0.
Example
Example2