C Sharp Basics – C# Sample Program

A Real World Problem is always solved by any Programming Language, and to solve a Real-World Problem, various necessary data related to that Real World Problem has to be input into the computer so that the computer can program instructions on that data. Can Generate Appropriate Output by doing Required Processing on Aadhaar.

For example, when we want to create a program to add two numbers, then both the numbers to be input for addition are of a type Number type data.

There are actually two types of data, which are known as Primary Data and Secondary Data, and in computer programming languages, ​​these data are commonly known as Standard Data Types and User-Defined Data Types.

Integer, Character, Long, Double, etc. Data types come under Standard Data Type, and the compiler of Programming Language understands these Primary Data Types internally. So when we declare a Variable of Integer Type, the compiler of the programming language knows that we want to reserve space in memory to process with an integer number.

But generally, not all types of Real World Problems are as simple as adding two numbers. Therefore a programmer cannot always develop programs by primary data types to handle complex types of problems. Because in Real-World Problems, there is always a need to process a group of logically related data of many types among themselves.

As a result, there is a need for such programming patterns, through which the “Logically Related Group of Data” related to Real World Problems can be accessed and manipulated in the form of a Data Bunch. An object-Oriented Programming System was developed to meet this need.

An object-Oriented Programming System is a programming pattern that allows the objects and data related to a real-world problem to be represented in the computer exactly in the same way as the objects and data related to that problem are in the real world.

Also, by this OOPS Programming Pattern, the problem in the computer is processed and solved exactly in the same way as the same problem is processed and solved in the real world.

In an Object-Oriented Programming System, any Real World Problem is always divided into Objects, and the way those Objects together generate an Appropriate Solution to any problem in the Real World, by OOPS Rules in Computer also related to that problem All the objects are made to interact with each other in the same way as the solution of that problem is generated in the real world.

For example, depositing Amount in Bank A/c is a type of problem. As a solution to this problem, the client goes to his bank branch and fills a deposit form, and gives the deposit form and amount to the cashier.

The cashier checks the amount on the basis of that Filled Deposit Form and makes the entry of that amount in the client’s account, and returns a part of the filled form as receipt again to the same client, which is There is an indication that the amount of the client has been deposited in his account.

Exactly when this problem has to be solved by the computer, then on the basis of the rules of OOPS, the problem is divided into different objects, and the interaction is done between those objects in the same way as to solve that problem. For in the Real World, different objects interact with each other.

If we understand this process by the previous example, then the Client Object goes to its Bank Branch Object and fills a Deposit Form Object, and gives the Deposit Form Object and Amount Information to the Cashier Object.

The Cashier Object checks the various information related to the deposit on the basis of that Filled Deposit Form Object and makes the entry of that information in the Account Object of the Client Object and a part of the Filled Form Object as the Receipt Object again in the same Client Object returns the.

To define different types of objects related to a Real-World Problem, in Object-Oriented Programming languages like C#, we have to create User Defined Data Types because these User Defined Data Types are the same as a Real-World Object in Computer Program Exactly the same way. Let us represent them the way those objects are in the real world.

C# Programming Language provides many programming constructs named Class, Interface, Structure, Enumeration, or Delegate to define a new type of User-Defined Data Type.

C# is a Pure Object-Oriented Programming Language. Therefore, if we have to write a single line of code, then that code has to be written in the form of some class definition, which is only a type of type definition.

Class Creation is the basic construct of any Pure Object-Oriented Programming Language. To understand it properly, we need to understand all the concepts of Object-Oriented Programming Systems thoroughly, and we will discuss these concepts in detail in the upcoming chapters of the book. Will understand

But at the moment, we are trying to understand only those Basic Object-Oriented Programming System concepts which are needed to create a simple C# program so that we can understand the various programming constructs of C# programming language through small programs and The Basic Structure of a Simplest C# Program is as follows:

using NamespaceName;

By this statement, we include the definition of the types used in our current program.

As we learned while discussing Microsoft.NET Framework, .NET Framework provides us with many BCL and FCL classes, in which thousands of Types (Classes, Interfaces, Structures, Enumerations, Delegates, etc.…) are already defined. And if we want to use these pre-made types in our current C# program, then it is necessary that we tell the C# compiler that the type we are using in our current C# program, in which library? has been defined.

In the .NET Framework, a group of different types related to the same type of functionality is kept enclosed between a programming construct named Namespace, which we can understand similar to the package of Java.

For example, in the .NET Framework, all the necessary types related to developing a GUI Frontend are stored in a namespace named System.Windows.Forms. Therefore, if we want to create a GUI Form Object in our current C# program, then we need to specify the Namespace named System.Windows.Forms in our current program with the using Keyword.

We can also understand Namespace similar to Header Files of C / C ++ Programming Languages, in which to use Defined Functions / Classes, we need to include those Header Files in our current C / C ++ Programs, in which those Functions / Classes have been defined, which we want to use in our current program.

namespace NewNamespaceName {

Whenever we create a program in C#, as we discussed in the previous section, to solve different types of Real-World Problems, we have to create different types of new User Defined Data Types because these types are the same. There is a way to represent the Objects related to the Real World Problem as Exactly Similar Objects in the Computer Program.

But thousands of types have already been defined in different namespaces in the .NET Framework. Therefore, when we create our program, then there is a possibility that the new type we are creating, some type of that name, already exists in these namespaces. In this situation, there is a possibility of name conflict, which may reflect as an error in our C# program.

Name Conflict represents an error that confuses the C# compiler as to which type to use. That which already exists in the .NET Framework or that which we have defined in the current program and generates compiler error when this type of confusion occurs.

There is no name conflict error, so C# allows us to create a new namespace. When we specify a Unique NewNamespaceName with a namespace Keyword, the type of the same name we define between the Curly Braces of this new Namespace if it already exists in a Namespace in the .NET Framework Library. Even then, the C# compiler does not generate any kind of error. Because Classes of the same name defined in two different namespaces, the C# compiler also identifies them differently due to being different namespaces.

Also, by using Namespace Concept, we can group all the types of new types according to our need on the basis of their functionalities and compile them as EXE or DLL assembly and reuse them again if needed in the future, which is also the basic concept of Object Oriented Programming System.

class MainClassName {

In C#, we cannot create Global Functions or Global Variables like C/C++ Programming Language; rather, we need to keep all Data and Functions Enclosed between Class Blocks like Java Programming Language and thus Enclosed Data to Data Members. And Functions are known as Methods.

A class is necessary for a C# program, which has a class named Main(), WinMain(), or DllMain() because it is the method that the CLR first executes as the entry point of the program execution. Whereas apart from this Main Class, we can define many more classes and other types in the same Namespace, and all these types are accessible by the same Namespace.

public static void Main(String[ ] args)

This method is the entry point of a console mode application, which the CLR executes by searching first in the current assembly. If Windows Application is being developed, then instead of this Main() Method, WinMain() method is specified, whereas if DLL Library is to be created, then DllMain() Method is specified as Entry Point.

When we create a Console Mode Application, we always keep the Main() method public so that the Main() method is publicly available for the CLR to be directly accessed.

Whereas the Main() method is always used with static Keyword only because the Main() method is a method that can be defined only once in any C# application, and it can be defined as Compulsory in every .NET application. Define is essential. Because this method is the entry point for the CLR, from where the execution of the C# program starts. Also, only the static method is a method that can be accessed directly by CLR without creating an object of the class.

Whereas in a Pure Object Oriented Programming Language like C# and Java, Global Function can never be created like C/C++, so Main() Function is also always defined inside Class and Function defined inside ClassClass. Calls can always be made only through an object of that ClassClass.

But the functions defined inside a class with which static Keyword is used those functions can be called directly without creating the object of that ClassClass.

That’s why static Keyword is used with the Main() Method in C# so that CLR can access the current C# Program by directly accessing the Main() Method of our Current Program, which is the Entry Point of our Console Program for CLR. Able to execute.

When we run after compiling our Console Mode Program, then while running, we can also pass Command Line Arguments according to our need from Command-Line to our Console Mode Programs.

All the arguments that we pass from the command line are stored in an array named args of string type of this Main() method, which we can use in our C# program as per our need.

Whereas in the body of this Main() method, we have to keep those codes enclosed, which represent our original C# program.

If we try to understand this C# Program Anatomy by a Simple Program, then our First Practical C# Program can be something as follows:

// File Name: HelloCSharp.cs
using System;
namespace CSharpFundamentals
class HelloCSharp
private static void Main(string[] args)
Console.WriteLine("Hello C#.");

In this program, first of all, we have specified System Namespace with using Keyword. Types related to Basic Input/Output and Collections have been defined in the System Namespace, which are required compulsorily for any Basic C# program.

Then we have created a namespace named CSharpFundamentals so that whatever new type we create will bind to this Namespace.

Then we have created a class named HelloCSharp, which is a kind of new User-Defined Data Type. Because C# is a Pure Object Oriented Programming Language and all the codes are written in it are part of some type, i.e., Encapsulated as Type.

Since the ClassClass we have created is named HelloCSharp, we have to make the same ClassClass as the main ClassClass of our program, so within this ClassClass, we have also defined the Main() method.

Whatever code we write between the Curly Braces of the Main() method, the C# Compiler sends the same code to the CLR to be executed because the Main() method itself is the Entry Point of Execution for the CLR.

Since we have “Hello C#.” as a simple message in our Console. We want to display the message, so we have used the Console.WriteLine() statement as follows:

        Console.WriteLine("Hello C#.");

In this statement, Console is the ClassClass that represents the keyboard and screen of our computer, i.e., Standard Input and Output Devices, and this ClassClass is defined in the Namespace named System, which we have created in our C# program through using Keyword so that we can use the data and methods of the ClassClass named Console.

Methods named ReadLine() and WriteLine() have been defined in this ClassClass named Console, using which we can receive data from the keyboard as input in our current C# program and process the result as Output. I can display it on the screen.

That is, the ReadLine() method represents the keyboard while the WriteLine() method represents the screen. That’s why when the CLR executes the above code line, we get “Hello C#” as OutputOutput on the screen. The message appears.

Leave a Comment