Terminologies in C Sharp

2. Terminologies in C Sharp .

a. Structure

Structure is a user-defined value type which encapsulates member data and member function.

A struct is a user-defined value type. It is declared in a very similar way to a class, except that it can't inherit from any class, nor can any class inherit from it (as mentioned previously, however, all value types do inherit from System.object. It capsules all the variable under one roof .

b. Scope of variable

In the body of a method, you can also declare variables that would be used internally. A variable declared in the body is referred to as a local variable. It cannot be accessed outside of the method it belongs to. After declaring a local variable, it is made available to the method and you can use it.

 C# doesn't have the notion of global variables (in C/C++, Visual Basic, Pascal, etc, a global variable is one that is declared outside of any class; such a variable is made available to any function, or even file, of the same program without being declared again where needed). Still, if you want to use the same variable in various methods of the main class, you can declare it outside of any method. The variable must be declared as static. That is, you must type the static keyword to its left when declaring it. Here is an example:

Using System;

Class Exercise

{

Static double Length;

Static void welcome()

{

console. WriteLine ("welcome to the wonderful world of c#");

}

Static void Main ()

{

Welcome();

console. WriteLine ();

}

}

After declaring such a variable, you can access from any method that belongs to the same class. Here is an example, to understand the concept of static variable.

Use of static variable

using System;

class Cylinder

{

static double Length;

static double Width;

static double Area;

static double Get The Length ()

{

double Len;

console. Write ("Length: ");

Len= double. Parse (console. Read Line ());

return 1en;

}

static double GetThewidth()

{

double w;

console. Write ("width: ");

w = double. Parse (console. Read Line ());

return w;

}

static void AreaRect ()

{

console. WriteLine ("Enter the Length and width of Rectangle ");

width = Get The width();

Length = Get The Length();

Area = Length * width;

}

static void show Area()

{

console. Write Line(" characteristics of Rectangle");

console. write Line("Length: "+ Length);

console. Write Line("width :"+ width);

Console. Write Line("Area :"+ Area); }

}

static void Main()

{

AreaRect ();

Show Area() ;

console. Write Line (); }

}

 }

OUTPUT:

Enter the Length and Width of Rectangle

Length: 38.24

Width: 32.58

Characteristics of Rectangle

Length: 38.24

Width: 32.58

Area : 1245.85

c. Namespace in C#

When many people work in creating the same program, it could be difficult to keep track of the names of various classes. If more than one programmer creates a class with the same name in the same program, there would be conflict and the program would not work. The solution to avoid this situation is to delimit sections of code with names.

A namespace is a collection of classes. Namespaces in C# are used to organize too many classes so that it can be easy to handle the application.

d. Identifiers 

Identifiers refer to the names of variables, functions arrays, classes, etc. created by programmer. They are fundamental requirement of any language. Each language has its own rules for naming these identifiers. To name the variables of your program, you must follow strict rules. In fact, everything else in your program must have a name.

e. Command Line Arguments

Command line arguments are parameters supplied to the Main method at the time of invoking it for execution. To understand the concepts see the example given below:

Program of Command line argument to accept name from the command line and writes to the console. (Sample.cs)

using System;

class sample

{

public static void Main( string[ ] args)

{

console.write("welcome to");

console.write(" " +args[0]);

console.write(" " +args[l]);

}

}

Execution for this program

C: > Sample My Home

Output of Example

Welcome to My Home

In Previous examples, we have used the Main method with no parameters. Notice that in the above example how the Main is declared.

Main is declared with a parameters args. The parameter args is an array of strings. Any arguments provided in the command line at the time of execution, are passed to the array args as its elements. We can access the array elements by using a subscript like args[0],args[1] and so on.

Like as in the command line" C:> Sample My Home", args[O] and args[1] contains:

Args [0] <-- My

args [1] <-- Home

There fore the values will be display of these parameters in Console.Write() statement.

Leave Comment

Important Topics

Title
Terminologies in C Sharp
Introduction to C Sharp
Hello World Program
C vs C Sharp
C Sharp vs C Plus Plus
C SHARP vs JAVA
Array and Types of Array
Programs - Sum of all numbers in Array
Strings and Types of Strings
Delegates in C sharp
System Collection Generic
System.Collections Classes
System.Collections.Concurrent
Dot Net Assembly
Events in C Sharp