Topics Covered:
A brief intro to C#
Features of C#
Data Types and Variables
Hello World Program
Interview Questions on the above topics
A brief intro to Csharp
C# (pronounced "see sharp") is a modern, object-oriented programming language developed by Microsoft in the early 2000s as part of its .NET initiative.
C# is designed to be simple, yet powerful, with syntax similar to that of other C-style languages such as C++, Java, and JavaScript.
C# has become a popular language for building various types of applications, including desktop applications, web applications, mobile apps, video games, and even IoT devices.
C# is an object-oriented, component-oriented programming language. C# provides language constructs to directly support these concepts, making C# a natural language in which to create and use software components. Since its origin, C# has added features to support new workloads and emerging software design practices. At its core, C# is an object-oriented language. You define types and their behaviour.
Features of Csharp
Object-oriented programming: C# is designed to support object-oriented programming concepts such as encapsulation, inheritance, polymorphism etc. This makes it a powerful language for building complex applications.
Garbage collection: C# has built-in garbage collection, which automatically manages memory allocation and deallocation, reducing the risk of memory leaks and freeing developers from manual memory management.
Delegates and events: C# supports delegates and events, which enable developers to write more modular, extensible code. Delegates allow methods to be passed as parameters to other methods, while events allow objects to raise notifications when certain actions occur.
LINQ: C# supports Language Integrated Query (LINQ), which enables developers to write queries that operate on collections of data, including databases, XML documents, and in-memory objects. LINQ provides a powerful and expressive syntax for querying data.
Asynchronous programming: C# supports asynchronous programming, which enables developers to write more responsive and scalable applications by executing long-running tasks in the background. Asynchronous programming allows the application to continue processing other requests while waiting for I/O operations to complete.
Type safety: C# is a type-safe language, which means that it provides compile-time type checking to catch type-related errors before the application is run. This reduces the risk of runtime errors and improves the overall reliability of the application.
Cross-platform support: C# can be used to build applications that run on multiple platforms, including Windows, Linux, and macOS. This is made possible by the .NET Core runtime, which provides a cross-platform implementation of the .NET framework.
Data Types
Data types are used to define the type of data that a variable can hold.
Data Type | Size (bytes) | Range of Values |
bool | 1 | true or false |
byte | 1 | 0 to 255 |
sbyte | 1 | -128 to 127 |
char | 2 | Unicode characters |
short | 2 | -32,768 to 32,767 |
ushort | 2 | 0 to 65,535 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
uint | 4 | 0 to 4,294,967,295 |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | 8 | 0 to 18,446,744,073,709,551,615 |
float | 4 | Approximately ±1.5 x 10^-45 to ±3.4 x 10^38 with 7 significant figures |
double | 8 | Approximately ±5.0 x 10^-324 to ±1.7 x 10^308 with 15 or 16 significant figures |
decimal | 16 | Approximately ±1.0 x 10^-28 to ±7.9 x 10^28 with 28 significant figures |
string | N/A | A sequence of Unicode characters |
Few Examples:
int myNumber = 42;
double myDouble = 3.14159;
bool isTodayMonday = true;
char myChar = 'A';
string myString = "Hello, world!";
Variables
In C#, a variable is a named memory location that can hold a value of a specific data type. Variables are used to store values that can be used later in a program, and they provide a way to refer to data by a name instead of its memory location.
To create a variable in C#, you first need to declare it with its data type, and then optionally initialize it with a value.
Here's an example of declaring and initializing a variable:
int a; // declaring a variable of type int
a = 42; // initializing the variable with the value 42
Alternatively, you can declare and initialize a variable in a single line, like this:
int a = 42; // declaring and initializing a variable of type int
Once a variable is declared and initialized, you can use it in your program. Here's an example of using a variable in a simple program that calculates the sum of two numbers:
int num1 = 5;
int num2 = 7;
int sum = num1 + num2;
Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum);
In this example, we declare and initialize two variables of the type int
called num1
and num2
, and then we add them together and store the result in a variable called sum
. Finally, we use the Console.WriteLine
method to print out the result. Note the use of placeholders {0}
, {1}
, and {2}
in the string, which corresponds to the variables num1
, num2
, and sum
, respectively.
Hello, World! Program
namespace Basics
{
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Output:
This program uses the Console.WriteLine()
method to output the text "Hello, World!" to the console.
The Main()
method is the entry point of the program and is executed when the program starts running.
Note that the using
statement at the beginning of the program imports the System
namespace, which contains the Console
class used in the program. Without the using
statement, you would need to prefix the Console
class with the namespace like this: System.Console.WriteLine("Hello, World!");
.
Namespace?
In C#, a namespace is a way to organize code into logical groups or scopes. A namespace can contain classes, interfaces, structures, enumerations, and other namespaces, and is used to avoid naming conflicts and to make it easier to manage and maintain code.
You can declare a namespace using the namespace
keyword followed by the namespace name, like this:
namespace MyNamespace
{
// some code
}
An example of one of the namespace is system
which we have seen earlier in Hello, World Program.
In C#, the System
namespace is a fundamental part of the .NET framework and provides access to a wide range of classes, interfaces, and structures that are used in many different types of applications.
The system
namespace includes classes for working with basic data types such as integers and strings, as well as more advanced functionality such as file input/output, threading, and networking. Some of the most commonly used types in the System
namespace includes:
Object
: The base class for all objects in C#.String
: A class used to represent text as a sequence of Unicode characters.Console
: A class that provides methods for working with the console, such as reading and writing text to the console window.Math
: A class that provides mathematical functions such as trigonometry and logarithms.DateTime
: A structure that represents a date and time value.Exception
: The base class for all exceptions in C#.
In addition to these basic types, the System
namespace also includes many other classes and types that provide functionality for a wide range of tasks.
Naming Conventions
In C#, naming conventions are used to make code more readable and maintainable. They help developers understand the purpose of variables, methods, classes, and other code elements, and make it easier to collaborate with other developers.
Here are some naming convention rules commonly used in C#:
PascalCase for class and method names: The first letter of each word in a class or method name should be capitalized. For example:
MyClass
,CalculateSum
.camelCase for variable and parameter names: The first letter of the first word should be lowercase, and the first letter of each subsequent word should be capitalized. For example:
myVariable
,calculateSum(int num1, int num2)
.All uppercase for constants: Constants should be written in all uppercase letters, with underscores between words. For example:
MAX_NUMBER
,PI
.Use descriptive names: Choose names that accurately describe the purpose of the code element. For example,
totalPrice
is more descriptive thantp
.Avoid abbreviations: Try to avoid abbreviations that may be unclear or confusing to others. For example, use
numberOfItems
instead ofnumItems
.Don't use reserved keywords: Avoid using reserved keywords as names for variables, methods, or classes. Reserved keywords are words that have a special meaning in the C# language, such as
if
,else
,while
,int
, andstring
.Use singular nouns for class names: Class names should be singular nouns that accurately describe the purpose of the class. For example,
Car
,Person
,Book
.
Interview Questions on the above topics
Introduction to C#:
What is C# and what is it used for?
What are the features of C#?
What is the difference between C# and .NET?
What is the difference between C# and Java?
Data Types:
What is a data type?
What are the different data types in C#?
What is the difference between value types and reference types?
Explain the difference between int and float data types.
What is the maximum and minimum value that can be stored in an int data type?
Variables:
What is a variable?
What are variable declaration and initialization?
Explain the difference between local and global variables.
What is the scope of a variable?
What is the difference between a variable and a constant?
Namespaces:
What is a namespace?
Why are namespaces used in C#?
What is the difference between system-defined namespaces and user-defined namespaces?
How do you declare a namespace in C#?
Can two namespaces have the same name? If so, how can they be differentiated?
Thank you for reading my Blog. I hope you have learned something from it!
If you find this blog helpful, please like, share, and follow me for more interesting posts like this in the future.