SlideShare a Scribd company logo
C# Tutorial
Part 5: Variable, Datatype,
Keywords
www.sirykt.blogspot.com
• 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.
• Static variable:
• Static variables memory allocates in the class memory
• The static variable value we can maintain continuously until closing
the application
• We can't destroy static variable value
• The static variable features we can share with all class objects
• We can saves the application memory purpose using static variable
• Normal variable:
• Normal variables memory allocates in the objects memory
• This value we can't maintain continuously until closing the app
• We can destroy the normal variable value
• The normal variable features we can't share with class objects
• We can't saves the application memory.
example on static & normal variable
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace ConsoleApplication20
• { class m1//class name
• { static int a, b, c;//static variables
• public void add()//non static method
• { a = 100;
• b = 200;
• c = a + b;
• Console.WriteLine(c.ToString());
• Console.ReadLine(); }
• public static void display()//static method
• { a = 500;
• b = 500;
• c = a + b;
• Console.WriteLine(c.ToString());
• Console.ReadLine(); }
• static void Main(string[] args)
• { m1 obj = new m1();//non static methods create the instance
• obj.add();//object through calling the method
• m1.display();//static variable directly access by the class
name
• } } }
• note:
• A static class is basically the same as a non-static class, but there is
one difference: a static class cannot be instantiated.
• In other words, we cannot use the new keyword to create a variable of
the class type.
• Because there is no instance variable, we access the members of a static
class by using the class name itself
• A data type specifies the type of data that a variable can store such as integer,
floating, character etc.
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
Types Data Types
Value Data Type int, char, float, Boolean, etc
Reference Data Type String, Class, Object and Interface
Pointer Data Type Pointers
Value Data Type
• The value data types are integer-based and floating-point based. C#
language supports both signed and unsigned literals.
• There are 2 types of value data type in C# language.
• 1) Predefined Data Types - such as Integer, Boolean, Float,
etc.
• 2) User defined Data Types - such as Structure,
Enumerations, etc.
Data Types Memory Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
short 2 byte -32,768 to 32,767
signed short 2 byte -32,768 to 32,767
unsigned short 2 byte 0 to 32,767
int 2 byte -32,768 to 32,767
signed int 2 byte -32,768 to 32,767
unsigned int 2 byte 0 to 32,767
short int 2 byte -32,768 to 32,767
signed short int 2 byte -32,768 to 32,767
unsigned short int 2 byte 0 to 32,767
long int 4 byte
signed long int 4 byte
unsigned long int 4 byte
float 4 byte
double 8 byte
long double 10 byte
Reference Data Type
• The reference data types do not contain the actual data stored in a
variable, but they contain a reference to the variables.
• If the data is changed by one of the variables, the other variable
automatically reflects this change in value.
• There are 2 types of reference data type in C# language.
• 1) Predefined Types - such as Objects, String.
• 2) User defined Types - such as Classes, Interface.
Pointer Data Type
• The pointer in C# language is a variable, it is also known as locator or indicator
that points to an address of a value.
Symbol Name Description
& (ampersand sign) Address operator Determine the address of a variable.
* (asterisk sign) Indirection operator Access the value of an address.
Print the limits of datatypes
• using System;
• namespace printthelimitsofdatatypes
• { class Program
• { static void Main(string[] args)
• { Console.WriteLine(byte.MinValue.ToString());
• Console.WriteLine(byte.MaxValue.ToString());
• Console.WriteLine(int.MinValue.ToString());
• Console.WriteLine(int.MaxValue.ToString());
• Console.WriteLine(short.MinValue.ToString());
• Console.WriteLine(short.MaxValue.ToString());
• Console.WriteLine(long.MinValue.ToString());
• Console.WriteLine(long.MaxValue.ToString());
• Console.WriteLine(float.MinValue.ToString());
• Console.WriteLine(float.MaxValue.ToString());
• Console.WriteLine(double.MinValue.ToString());
• Console.WriteLine(double.MaxValue.ToString());
• Console.WriteLine(decimal.MinValue.ToString());
• Console.WriteLine(decimal.MaxValue.ToString());
• Console.ReadLine();
• }}}
o/p:
• 0
• 255
• -2147483648
• 2147483647
• -32768
• 32767
• -9223372036854775808
• 9223372036854775807
• -3.402823E+38
• 3.402823E+38
• -1.79769313486232E+308
• 1.79769313486232E+308
• -79228162514264337593543950335
• 79228162514264337593543950335
Keywords
• A keyword is a reserved word. You cannot use it as a variable name, constant
name etc.
• In C# keywords cannot be used as identifiers.
• However, if we want to use the keywords as identifiers, we may prefix the
keyword with @ character.
abstract base as bool break catch case
byte char checked class const continue decimal
private protected public return readonly ref sbyte
explicit extern false finally fixed float for
foreach goto if implicit in in (generic
modifier)
int
ulong ushort unchecked using unsafe virtual void
null object operator out out (generic
modifier)
override params
default delegate do double else enum event
sealed short sizeof stackalloc static string struct
switch this throw true try typeof uint
abstract base as bool break catch case
volatile while
SIRYKT
Sharing Knowledge is Learning
For more updates
For more visit our website www.sirykt.blogspot.com

More Related Content

5variables in c#

  • 1. C# Tutorial Part 5: Variable, Datatype, Keywords www.sirykt.blogspot.com
  • 2. • 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.
  • 3. • Static variable: • Static variables memory allocates in the class memory • The static variable value we can maintain continuously until closing the application • We can't destroy static variable value • The static variable features we can share with all class objects • We can saves the application memory purpose using static variable
  • 4. • Normal variable: • Normal variables memory allocates in the objects memory • This value we can't maintain continuously until closing the app • We can destroy the normal variable value • The normal variable features we can't share with class objects • We can't saves the application memory.
  • 5. example on static & normal variable • using System; • using System.Collections.Generic; • using System.Linq; • using System.Text; • using System.Threading.Tasks; • namespace ConsoleApplication20 • { class m1//class name • { static int a, b, c;//static variables • public void add()//non static method • { a = 100; • b = 200; • c = a + b; • Console.WriteLine(c.ToString()); • Console.ReadLine(); } • public static void display()//static method • { a = 500; • b = 500; • c = a + b; • Console.WriteLine(c.ToString()); • Console.ReadLine(); } • static void Main(string[] args)
  • 6. • { m1 obj = new m1();//non static methods create the instance • obj.add();//object through calling the method • m1.display();//static variable directly access by the class name • } } } • note: • A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. • In other words, we cannot use the new keyword to create a variable of the class type. • Because there is no instance variable, we access the members of a static class by using the class name itself
  • 7. • A data type specifies the type of data that a variable can store such as integer, floating, character etc.
  • 8. 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
  • 9. Types Data Types Value Data Type int, char, float, Boolean, etc Reference Data Type String, Class, Object and Interface Pointer Data Type Pointers
  • 10. Value Data Type • The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals. • There are 2 types of value data type in C# language. • 1) Predefined Data Types - such as Integer, Boolean, Float, etc. • 2) User defined Data Types - such as Structure, Enumerations, etc.
  • 11. Data Types Memory Size Range char 1 byte -128 to 127 signed char 1 byte -128 to 127 unsigned char 1 byte 0 to 127 short 2 byte -32,768 to 32,767 signed short 2 byte -32,768 to 32,767 unsigned short 2 byte 0 to 32,767 int 2 byte -32,768 to 32,767 signed int 2 byte -32,768 to 32,767 unsigned int 2 byte 0 to 32,767 short int 2 byte -32,768 to 32,767 signed short int 2 byte -32,768 to 32,767 unsigned short int 2 byte 0 to 32,767 long int 4 byte signed long int 4 byte unsigned long int 4 byte float 4 byte double 8 byte long double 10 byte
  • 12. Reference Data Type • The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables. • If the data is changed by one of the variables, the other variable automatically reflects this change in value. • There are 2 types of reference data type in C# language. • 1) Predefined Types - such as Objects, String. • 2) User defined Types - such as Classes, Interface.
  • 13. Pointer Data Type • The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value. Symbol Name Description & (ampersand sign) Address operator Determine the address of a variable. * (asterisk sign) Indirection operator Access the value of an address.
  • 14. Print the limits of datatypes • using System; • namespace printthelimitsofdatatypes • { class Program • { static void Main(string[] args) • { Console.WriteLine(byte.MinValue.ToString()); • Console.WriteLine(byte.MaxValue.ToString()); • Console.WriteLine(int.MinValue.ToString()); • Console.WriteLine(int.MaxValue.ToString()); • Console.WriteLine(short.MinValue.ToString()); • Console.WriteLine(short.MaxValue.ToString()); • Console.WriteLine(long.MinValue.ToString()); • Console.WriteLine(long.MaxValue.ToString()); • Console.WriteLine(float.MinValue.ToString()); • Console.WriteLine(float.MaxValue.ToString()); • Console.WriteLine(double.MinValue.ToString()); • Console.WriteLine(double.MaxValue.ToString()); • Console.WriteLine(decimal.MinValue.ToString()); • Console.WriteLine(decimal.MaxValue.ToString()); • Console.ReadLine(); • }}}
  • 15. o/p: • 0 • 255 • -2147483648 • 2147483647 • -32768 • 32767 • -9223372036854775808 • 9223372036854775807 • -3.402823E+38 • 3.402823E+38 • -1.79769313486232E+308 • 1.79769313486232E+308 • -79228162514264337593543950335 • 79228162514264337593543950335
  • 16. Keywords • A keyword is a reserved word. You cannot use it as a variable name, constant name etc. • In C# keywords cannot be used as identifiers. • However, if we want to use the keywords as identifiers, we may prefix the keyword with @ character.
  • 17. abstract base as bool break catch case byte char checked class const continue decimal private protected public return readonly ref sbyte explicit extern false finally fixed float for foreach goto if implicit in in (generic modifier) int ulong ushort unchecked using unsafe virtual void null object operator out out (generic modifier) override params default delegate do double else enum event sealed short sizeof stackalloc static string struct switch this throw true try typeof uint abstract base as bool break catch case volatile while
  • 19. For more updates For more visit our website www.sirykt.blogspot.com