Indrocution To C#
Indrocution To C#
2. **Type Safety**: C# enforces type checking, meaning that the data types are checked
at compile time to prevent errors. This ensures that variables hold only the intended
type of data, reducing common programming mistakes.
Each of these features helps make C# a popular choice for developers building a variety
of applications across different platforms.
Basic structure of c#
1. Namespace Declaration
• A namespace is a way to organize classes and prevent naming conflicts.
• The using keyword imports necessary libraries or namespaces.
csharp
Copy code
using System; // Commonly used for basic functionality (like input/output).
2. Class Declaration
• The class keyword defines a new class, which is the building block of a C#
program.
• Each program has at least one class containing methods and variables.
csharp
Copy code
namespace HelloWorldApp
{
public class HelloWorld
{
// Fields, properties, methods, and events can go here.
}
}
3. Main Method
• The Main method is the entry point of any C# program.
• It must be static and can return either void or int.
• This is where the program execution begins.
csharp
Copy code
public static void Main(string[] args)
{
// Code execution starts here.
}
4. Statements and Expressions
• Statements and expressions go within the Main method or other methods.
• This is where the logic of the program is implemented.
csharp
Copy code
Console.WriteLine("Hello, World!"); // Print to the console.
5. Methods and Functions
• Besides the Main method, additional methods can be defined to organize code
into reusable blocks.
• Methods can be public or private, and they may return values or be void.
csharp
Copy code
public void GreetUser()
{
Console.WriteLine("Welcome to the C# program!");
}
6. Comments
• Comments are non-executable statements used for documentation.
• They make code easier to understand and maintain.
csharp
Copy code
// This is a single-line comment.
1.documentation section
2.using directive section
3.interface section
4.classes section
5.main method section
Documentation Section
• The documentation section is where comments are added to provide clear
descriptions of each part of the code.
• C# uses XML-style comments (starting with ///) to generate documentation for
classes, methods, properties, and parameters.
• Documentation comments improve code readability and serve as built-in
documentation that can be used with tools like IntelliSense in Visual Studio.
2. Using Directive Section
• The using directives specify the namespaces that the program needs to access
types and classes defined elsewhere, which helps avoid using fully qualified
names.
• This section comes at the very top of the code file and includes commonly used
namespaces, such as System for basic functionalities.
• Example
•
using System; // Provides basic functionality
using System.Collections.Generic; // Enables working with collections
3. Interface Section
• An interface defines a contract that classes implementing it must fulfill. It includes method
signatures without providing the implementation.
• This section typically appears after the using directives and before or within the main
namespace.
• Interfaces are named starting with an uppercase "I" by convention (e.g., IExample), and they help
define the structure for classes that implement them.
Ex:
public interface ICalculator
{
int Add(int a, int b);
int Subtract(int a, int b); }
Classes Section
• The class section is where the primary logic of the program is written.
• Classes are the building blocks of a C# program and contain fields, properties, methods, and
constructors.
• Classes can implement interfaces, inherit from other classes, and contain multiple methods.
public class Calculator : ICalculator // Implements the ICalculator interface { public int Add(int
a, int b) { return a + b; } public int Subtract(int a, int b) { return a - b; } }
// Interface Section
public interface ICalculator
{
int Add(int a, int b);
int Subtract(int a, int b);
}
// Classes Section
public class Calculator : ICalculator
{
/// <summary>
/// Adds two integers.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The sum of the two integers.</returns>
public int Add(int a, int b)
{
return a + b;
}
/// <summary>
/// Subtracts the second integer from the first.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The difference between the two integers.</returns>
public int Subtract(int a, int b)
{
return a - b;
}
}
Visual Studio is one of the Microsoft IDE tools. Using this tool, we can develop, build, compile,
publish and run applications with the .NET framework. This tool provides some features such as
Editor
Compiler
Interpreters, and Many Morex
Step3
The next step is to configure the new project. Here, you need to provide the project
name and solution name. You can also give the same name to both project and solution
but it is not mandatory. Here, I am providing the name MyFirstProject to both project
and solution. You need to provide the location where you need to create the project.
Here, you also need to provide the .NET Framework version that you want to use in this
application. The latest version of the .NET Framework is 4.8. So, I am selecting .NET
Framework 4.8 and then clicking on the Create button as shown in the below image.
Once you click on the Create button, visual studio will create the Console Application
with the following structure.
A project called MYFirstProject will be created in Visual Studio. This project will contain
all the necessary required files (Properties, References, App.Config files) to run the
Console application. The Main program called Program.cs is the default code file that is
created when a new console application is created in Visual Studio it contains the Main
method by default and from that Main method our application is going to start its
execution, but if you want you can also change this default behavior. So, if you look at
the Program.cs class file, then you will see the following code.
Step4
Now let’s write our code which will be used to display the message “Welcome to
C#.NET” in the console window. To do so, modify the Main method of the Program class
as shown in the below code.
Step5
The next step is to run the .NET Application. To run any program in Visual Studio, you just
need to click on the Start button or you can press CTRL+F5 as shown in the below
image.
Once you click on the Start button, you should get the following console window
showing the message.
Using Visual Studio, if we are creating a console application (excluding .NET 6), then
automatically we are getting four sections which are shown in the below image.
Importing Namespace Section:
This section contains importing statements that are used to import the BCL (Base Class
Libraries) as well as user-defined namespaces if required. This is similar to the included
statements in the C programming language. Suppose you want to use some classes and
interfaces in your code, then you have to include the namespace(s) from where these
classes and interfaces are defined. For example, if you are going to use the Console
class in your code, then you have to include the System namespace as the Console
class belongs to the System namespace.
Syntax: using NamespaceName;
Example: using System;
If the required namespace is a member of another namespace, we have to specify the
parent and child namespaces separated by a dot as follows:
using System.Data;
using System.IO;
Note: A namespace is a container that contains a group of related classes and
interfaces, as well as, a namespace can also contain other namespaces.
Namespace Declaration Section:
Here a user-defined namespace is declared. In .NET applications, all classes and
interfaces or any type related to the project should be declared inside some
namespace. Generally, we put all the related classes under one namespace and in a
project, we can create multiple namespaces.
Syntax: namespace NamespaceName {}
Example: namespace MyFirstProject {}
Generally, the namespace name will be the same as the project name but it is not
mandatory, you can give any user-defined name to the namespace.
Class Declaration Section:
For every Desktop Application in .NET, we need a start-up class file. For example, for
every .NET Desktop Application like Console and Windows, there should be a Start-Up
class that should have the Main method from where the program execution is going to
start. When we create a Console Application using Visual Studio, by default, Visual
Studio will Create the Start-Up class file with the name Program.cs which will have a
class with the name Program that contains the Main method. A start-up class is nothing
but a class that contains a Main() method from which the program execution is going to
start.
Syntax:
class ClassName
{
}
Example:
class Program
{
}
Note: You can change this default behavior. You can create your own class and you can
also make that class as the Start-Up Class by including the Main method. In our
upcoming articles, we will discuss this in detail.
Main() Method Section:
The main() method is the entry point or starting point of the application to start its
execution. When the application starts executing, the main method will be the first
block of the application to be executed. The Main method contains the main logic of the
application.
What is using?
Using is a keyword. Using this keyword, we can refer to .NET BCL in C# Applications i.e.
including the BCL namespaces as well as we can also include user-defined
namespaces that we will discuss as we progress in this course. Apart from importing
the namespace, other uses of using statements are there, which we will also discuss as
progress in this course. For now, it is enough.
Note: In .NET the base class libraries are divided into a collection of namespaces. Each
namespace contains a set of predefined classes and sub-namespaces. The namespace
contains another namespace called sub-namespaces.
Advantages of using the .NET framework from the C# point of view.
1. It provides GUI features. Earlier programming languages like C and C++ do not
support GUI features but C#.NET will provide complete GUI features. All GUI
features are getting from the framework.
2. We can connect with any database and perform the operations. Using ADO.NET
and Entity Framework technologies, we can perform the DB operations with any
Database. ADO.NET and Entity Framework are also a part of the .NET Framework.
3. The Framework also helps us to develop WEB Based applications. Using ASP.NET
technology we can develop WEB Based Applications. ASP.NET itself alone
cannot develop Web Applications; it requires language support. So, here we can
use C# as the programming language. ASP.NET is also a part of the framework.