Showing posts with label LearnCsharp. Show all posts
Showing posts with label LearnCsharp. Show all posts

Visibility Modifiers

Visibility Modifiers in C#
We have already encountered quite a number of so-called modifiers-keywords that can be applied to a type or to a member. Modifiers can indicate the visibility of a method, such as public or private, or the nature of an item, such as whether a method is virtual or abstract. C# has a number of modifiers, and at this point it’s worth taking a minute to provide the complete list.

Abstract Classes

C# allows both classes and functions to be declared as abstract. An abstract class cannot be instantiated, while an abstract function does not have an implementation, and must be overridden in any non abstract derived class.
Obviously, an abstract function is automatically virtual. If any class contains any abstract  functions, then that class is also abstract and must be declared as such. 
  • Abstract class declaration is:
abstract class MyBaseClass
{
       
}

Inheritance in C#

Inheritance allows you to create several classes that are distinct but share a common functionality. Specialized classes, called derived classes, inferit from a common class called the base class. A derived class is also said to extend a base class. The base class encapsulates the common functionality that will be present in each derived class, while the derived classes provide additional functionality specific to each class
  • If you want to declare that a class derives from another class, use the following syntax: 
   class MyDerivedClass : MyBaseClass
   {
     //Function and data members here

   }

Sealed Classes and Methods

Sealed Classes and Methods in C#C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method.

sealed class mClass
{
  // etc.


Destructors in C#

We have seen that constructors allow you to specify actions that must take place whenever an instance of a class is created. Conversely, destructors are called before an object is destroyed by the garbage collector. Given this behavior, a destructor would initially seem like a great place to put code to free unmanaged resources and perform a general cleanup. Unfortunately, things are not so straightforward.

Constructors in C#

The syntax for declaring basic constructors in C# is the same as in Java and C++. We create a method that has the same name as the containing class, and which does not have any return type:
public class MyClass
{
   public MyClass()
   {
   }
}

Properties in C#

Properties in C# is a member that provides  a flexible work to write, read or compute a value of a private field. Properties is one or two code blocks and representing a get and set accessor. For get accessor the code block executed when the property is read and for the set executed when the property is assigned a new value. Set property is only for reading and get property considered write – only.

Defining Properties

Class Introduction

C# Language is all about Object Oriented programming and classes. A class is a group of related methods and variables. Classes describes these things, and in most cases, you create an instance of this class, now referred to as an object, on this object, you use the defined methods and variables. The Windows Form itself is a Class, and when you run your programme, you are creating Objects , example: a Form object, a Textbox object etc. When define a class, define a design for a data type.

Class definition:

Methods in C#

Methods in C# is a code  block containing of statements.Methods are declared  within class or struct by specifying the access  level, the name of the method, the return value and any method parameters. To use  a method, you need to define the method and call the method. Syntax for defining a method in C# are:
Access Specifier(public or privateReturn Type Method Name(Parameter List)
{
    Body  Methods
{

C# Arrays

Arrays are a way to manage groups of similarly typed values or objects. With arrays, you can group a series of variables and refer to them with an index. You can loop through all or part of the variables and examine or affect each in turn. You also can create arrays with multiple dimensions. Arrays in the .NET Framework have built-in functionality to facilitate many tasks. Syntax is:

C# Loops

C# provides four different loops (for, while, do-while and foreachthat allow us to execute a block of code repeatedly until a certain condition is met. The for, while and do-while loops are essentially identical to those encountered in C++.

Conditional Statement in C#

C# has two constructs for braching code – the if statement, which allows us to test whether a specific condition is met, and the switch statement, which allows us to compare an experssion with a number of different values.

The if statement is one of the single most important  statements in every programming language. In C#, the if statement is very simple to use. The if statement needs a boolean result that is true or false.


The switch statement is like a set of if statements. It will be familiar to C++ and Java programmers and is similar to select case statement in Visual Basic.

C# Operators


Saving Your Work in C#

To save your work in visualC#.NET is too easy , first you need to click Files from the menu bar at the top of Visual Studio 2010 software and from the file menu click Save All or just ShortcutKeys  Ctrl + Shift + S:

Saving Your Work in C#

When you click Save All from the file menu you will save your work.

Variable Initialization in C#


The syntax for variable declaration in C# is:

datatype variablelist

C# Variables


In C# variables represent storage locations. All variables in C#  has a type. Local variables are variables that are declared in methods, properties etc. We declare variables in C# using the following syntax:

datatype identifier;

C# Type Conversion

C# Type Conversation

The converting type in C# is basically type casting , you will need to convert data from one type to another. Data can be converted in two forms:



  • Implicit - these conversions between types are performed whenever the conversion can occur without the loss of data.Examples are conversions from smaller to larger integral types, and conversions from derived classes to base classes.
  • Explicit -When performing a conversion where types cannot be implicitly converted, you must explicitly convert the types. This conversion is called a cast. Explicit conversions using a special syntax in Visual C#.

C# Data Types

    
C# Data Types
The Microsoft .NET Framework provides a robust system of primitive types to store and represent data in your application. Data types represent integer numbers, floating-point, numbers and non-numeric types. 



Create a Simple C# Console Application

The simple programme we will create is called a Console Application. First open the Visual Studio 2010 software from your  programs menu.


Create a Simple C# Console Application

C# Introduction

C# Tutorial Introduction
This programming language – C# is a simple, modern, object-oriented, structured language, easy to learn, and type-safe one. It will be very familiar to programmers of C and C++. C# is designed for “Common Language Infrastructure” (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages to be used on different computer platforms and architectures. C# combines the high productivity of Rapid Application Development (RAD) languages and the raw power of C++.