Advertisement
//C# Example of variables
using System;
class A
{
public static void Main(String[] ar)
{
//Declaring and initializing local variables
//With valid identifiers/variable names
int size = 1000;
float ink_prc = 9.50f;
double time = 19.965;
short _dist2 = 20;
char alpha_1 = 'a';
//Printing value of each variable
Console.WriteLine("The value in variable size is : " + size);
Console.WriteLine("The value in variable inc_prc is : " + ink_prc);
Console.WriteLine("The value in variable time is : " + time);
Console.WriteLine("The value in variable _dist2 is : " + _dist2);
Console.WriteLine("The value in alpha_1 is : " + alpha_1);
}
}
The value in variable size is : 1000
The value in variable inc_prc is : 9.5
The value in variable time is : 19.965
The value in variable dist2 is : 20
The value in alpha_1 is : a
//C# Example of variables
//C# Declaring and initializing multiple variables of the same type, in a single type
using System;
class A
{
public static void Main(String[] ar)
{
//Declaring and initializing multiple variables of the same type(int), in a single type
int a = 1000, b, c = 2000;
//Declaring and initializing multiple variables of same type(float), in a single
float ink_prc = 9.50f, weight;
//Declaring and initializing multiple variables of same type(char), in a single
char ch, alpha_1 = 'a';
}
}
| keywords | keywords | keywords | keywords |
|---|---|---|---|
| catch | bool | void | unsafe |
| typeof | this | static | sealed |
| readonly | params | operator | namespace |
| internal | int | foreach | finally |
| event | do | continue | char |
| break | abstract | as | byte |
| checked | decimal | double | explicit |
| fixed | is | goto | in |
| new | out | private | ref |
| string/String | short | throw | uint |
| ushort | volatine | base | case |
| class | default | else | extern |
| float | if | int | lock |
| null | out | protected | return |
| sizeof | struct | true | ulong |
| using | while | const | delegate |
| enum | false | implicit | for |
| long | interface | object/Object | override |
| public | sbyte | stackalloc | switch |
| try | unchecked | using static |
| keywords | keywords | keywords | keywords |
|---|---|---|---|
| yield | var | select | partial |
| join | global | dynamic | sync |
| when | where | set | partial |
| let | group | from | alias |
| await | ascending | descending | get |
| into | orderby | remove | value |
| where |
Advertisement
//C# Declaring and initializing a constant
using System;
class A
{
public static void Main(String[] ar)
{
//Declaring a constant with const keyword
//And initializing it with value 10
const int distance = 10;
//Printing the value of a constant
Console.WriteLine("The value of a constant 'distance' is : " + distance);
}
}
The value of a constant 'distance' is : 10
//C# You cannot declare and initialize a constant in separate statements.
using System;
class A
{
public static void Main(String[] ar)
{
//Only declaring a constant with const keyword
//But we have not initializing it with a value
const int distance;
//Initializing constant to a value in a different statement
distance = 30;
}
}
A.cs(11,12): error CS0145: A const field requires a value to be provided
//C# Trying to modify value of a constant
//Will issue a compile error
using System;
class A
{
public static void Main(String[] ar)
{
//Declaring a constant with const keyword
//And initializing it with value 10
const int distance = 10;
//Printing the value of a constant
Console.WriteLine("The value of constant distance is : " + distance);
//Trying to modify the value of a constant, distance
distance = 30;
}
}
A.cs(18,2): error CS0131: The left-hand side of an assignment must be a variable, property or indexer
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement