As we know that now a days C# plays a major role in most of the Web technologies to write the server side code, Ex: ASP.NET, ASP.NET MVC etc., Lets see Language Basics of C# in this chapter.
Introduction
So far we have seen
Identifiers
and
Keywords
of C# in previous article and let's see some more basics of
C# Language in here.
Objective
The main objective of this article is to learn literals
, operators
, punctuators
etc. in C# Programming.
Description
Let's recall the example code that we had in the previous article to explain few more things.
using System;
class Addition
{
static void Main()
{
int a = 15 + 25;
Console.WriteLine (x);
}
}
Literals:
Literals are the primitive pieces of data that are embedded into a program. i.e. representation of source code.
In the above example code, 15 and 25 are the literals
.
There are so many types of literals are there in C#. They are.
- Integer-literal - These are used to write values of type
int
, long
, uint
, ulong
etc. with decimal and hexadecimal forms.
- Boolean-literal - Boolean means
true
or false
. So there are two boolean literals: true
and false
.
- Character-literal - These literals represents a single character in quotes. Ex: 'x'.
- String-literal - There are two types of string literals in C#. They are
regular
and verbatim
. Regular means zero or more characters mentioned in double quotes, Ex: "Welcome". Verbatim means @ character followed by double quoted character, Ex: @"Welcome".
- Real-literal - These are used to write the values with type of
float
, decimal
and double
.
- Null-literal - It is simply a null type i.e.
null
.
Punctuators:
Punctuators are used to group or separate the part of code. These helps to demarcate the program structure.
In the above code, {
, }
and ;
are the punctuators.
- Braces are used to group the multiple statements into a separate block.
- Semicolon is used to terminate the statement.
Operators:
It combines the expressions or transforms them.
Ex: a + b.
'+' is called as an operator which combines a
and b
and performs addition.
In the above example code, .
, ()
, +
and =
are the operators.
There are several kinds of operators are there in C# which does different operations based on the operands(literals).
Type Basics
Type defines a blueprint for our values. Ex: Type of literals that we used in the above code.
There is something called variable which is a storage location that can handle different values over time.
'a
' is known as the variable in above program which locates the value.
Predefined Types:
These are specially supported by the compiler.
In the above code, 'int
' is a predefined type and is used to represent the Integer set that fit into 32 bits of memory.
int a = 15 + 25;
This will perform the addition (Arithmetic function).
There are some more predefined types say bool
, string
etc.
string
represents a sequence of characters. Ex: "DotNetFunda", "KidsFunda".
Example code using string,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello
{
class Program
{
static void Main()
{
string msg = "Hello, Welcome to C#";
string upperMsg = msg.ToUpper();
Console.WriteLine(upperMsg);
double a = 5.0;
msg = msg + " " + a.ToString();
Console.WriteLine(msg);
}
}
}
The output of the above program looks like,

bool
is used for two possible values either true
or false
. This is mostly used with if
statement to conditionally branch the flow of execution.
Observe the below example code with bool
type in it,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello
{
class Program
{
static void Main()
{
bool val = true;
if (val)
{
Console.WriteLine("This comes on the screen");
}
else
{
Console.WriteLine("This doesn't comes on the screen");
}
}
}
}
Now press Ctrl + F5 and run this code. You will see that only true
value is printed on the Console.

Custom Types:
Custom types means building Complex types from Primitive types. Observe the below example program in which we are using Custom types.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello
{
public class Test {
int x; // This variable is called as Field
// This is a Constructor
public Test(int y)
{
x = y;
}
// Addition Method
public int Add(int z)
{
return x + z;
}
}
class Program
{
static void Main() {
Test a = new Test (10); // Object
Console.WriteLine(a.Add(5)); // 10 + 5 = 15
Test b = new Test (20); // Object
Console.WriteLine (b.Add(10)); // 20 + 10 = 30
Console.WriteLine (a.Add(b.Add(5))); // 10 + (20 + 5) = 35
}
}
}
In the above code, we have a Constructor (
Test
) and a function (
Add
). We have created objects
a
and
b
in the main class.
Now press Ctrl + F5 and see the check the output of this program which may look like,
Conclusion
In this article we have seen Literals, Punctuators and Operators in
C#. And also covered Predefined and Custom types with examples. Hope you enjoyed it.
Thanks for reading.
Regards,
Krishna.